如何按字母顺序将数组放入 json?

How can I put array alphabetically in json?

我有以下 json 结构。

json = {
        "Canada": ["Toronto"],
        "Argentina": ["Buenos Aires"],
        "Brazil": ["Rio de Janeiro"],
        }

我正在寻找一种按 children 字母顺序排序的方法。应该是这样的:

json = {
        "Argentina": ["Buenos Aires"],
        "Brazil": ["Rio de Janeiro"],
        "Canada": ["Toronto"]

        }

有人可以帮帮我吗?我正在使用 jQuery.

对对象键进行排序,然后使用新的键顺序再次构建对象,但值相同:

Object.keys(json) // extract object keys in an array
   .sort()  // sort keys' array alphabetically 
   .reduce((result, key) => ({...result, [key]: json[key]}), {}) // build the same object but with the new order of keys