Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按国家分组JSON并输出按字母顺序排序的状态列表_Javascript_Json_Sorting_Recursion_Nested - Fatal编程技术网

Javascript 按国家分组JSON并输出按字母顺序排序的状态列表

Javascript 按国家分组JSON并输出按字母顺序排序的状态列表,javascript,json,sorting,recursion,nested,Javascript,Json,Sorting,Recursion,Nested,我想将输入的JSON按国家分组,并输出按字母顺序排序的国家列表,并按字母顺序列出每个国家的州名 //I would like the expected output in alphabetically sorted order in country // and if the country has more than 1 state, the state should be in sorted order as follows: var res=[ { "country": "A

我想将输入的JSON按国家分组,并输出按字母顺序排序的国家列表,并按字母顺序列出每个国家的州名

//I would like the expected output in alphabetically sorted order in country    
// and if the country has more than 1 state, the state should be in sorted order as follows:

var res=[
{
  "country": "A", "state": "aa",
  "country": "A", "state: "ca",
  "country": "B", "state": "aa",
  "country": "B", "state": "ca",
  "country": "C", "state": "bb",
  "country": "C", "state": "cb"
 }
]   

// GIVEN input variable in JSON format
 var input = [{
    "country": "A",
    "state": "aa",
    "information": [
        {
            "country": "B",
            "state": "aa"
        },
        {
            "country": "C",
            "state": "bb"
        },
        {
            "country": "C",
            "state": "cb",
            "information": [
                {
                    "country": "A",
                    "state": "ca"
                },
                {
                    "country": "B",
                    "state": "ca"
                }
            ]
        } 
      ]
}]; 
//我尝试了下面的递归函数,因为似乎有不止一个嵌套函数提供信息。我尝试将console.log记录为预期的输出,但它没有输出我想要的结果:

var res=[];
console.log(compare(input,0));

function compare (input, n) { 
  if(!input[0].information) return res;
  for(i=0; i<n; i++){
    res.push(
      { 
        country: input[i].country,
        state: input[i].state
      }
    )  
  }
  console.log("res"+res[0]);
  return compare(input[0].information, input[0].information.length)
}
var res=[];
日志(比较(输入,0));
函数比较(输入,n){
如果(!input[0]。information)返回res;

对于(i=0;i在您的代码中,
res
是一个数组,其中一个对象具有多个键,使用相同的名称(
country
state
),这是不可能的,因此我假设您指的是多个对象,但您忘记了
{
}
(假设是这样的情况,)那么一个解决方案是:

var input = [
    {
        "country": "A",
        "state": "aa",
        "information": [
            {
                "country": "B",
                "state": "aa"
            },
            {
            "country": "C",
            "state": "bb"
            },
            {
                "country": "C",
                "state": "cb",
                "information": [
                    {
                        "country": "A",
                        "state": "ca"
                    },
                    {
                        "country": "B",
                        "state": "ca"
                    }
                ]
            }
        ]
    }
];

var output = [];

function flattenInformation (countries) {
    var newCountries = [];

    for (var i = 0; i < countries.length; i++) {
        var country = countries[i];

        if (country.hasOwnProperty('information')) {
            var information = country['information'];
            newCountries = newCountries.concat(flattenInformation(information));
            delete country['information'];
        }

        newCountries.push(country);
    }

    return newCountries;
}

output = flattenInformation(input);
output.sort(function(a, b) {
    if(a.country < b.country) return -1;
    if(a.country > b.country) return 1;
    if(a.state < b.state) return -1;
    if(a.state > b.state) return 1;
    return 0;
});

console.log(output);//[{"country":"A","state":"aa"},{"country":"A","state":"ca"},{"country":"B","state":"aa"},{"country":"B","state":"ca"},{"country":"C","state":"bb"},{"country":"C","state":"cb"}]
var输入=[
{
“国家”:“A”,
“州”:“aa”,
“信息”:[
{
“国家”:“B”,
“州”:“aa”
},
{
“国家”:“C”,
“州”:“bb”
},
{
“国家”:“C”,
“州”:“cb”,
“信息”:[
{
“国家”:“A”,
“州”:“ca”
},
{
“国家”:“B”,
“州”:“ca”
}
]
}
]
}
];
var输出=[];
职能信息(国家){
var newCountries=[];
对于(变量i=0;ib.国家)返回1;
如果(a.stateb.state)返回1;
返回0;
});
console.log(输出);//[{“国家”:“A”,“州”:“aa”},{“国家”:“A”,“州”:“ca”},{“国家”:“B”,“州”:“aa”},{“国家”:“B”,“州”:“ca”},{“国家”:“C”,“州”:“bb”},{“国家”:“C”,“州”:“cb”}]

谢谢!!这太棒了。输出数组是正确的,但是上面的排序函数不能正常工作,我还在处理。如果你知道其他排序方法,请告诉我。谢谢!@user21我想帮助你,但我恐怕不明白这个问题。在我提供的代码中,有一个排序函数是错误的ng应用于
输出
。然后有一个
控制台。日志
用于
输出
,以及我执行时得到的输出的注释。你是说你没有得到与我相同的输出,还是说我误解了它应该如何排序?(如果是,请说明要求)很抱歉,我一定是打错了sublime中的a键。您的排序功能代码运行良好。非常感谢您的帮助!