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

时间:2016-10-20 04:58:34

标签: 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", "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)
}

1 个答案:

答案 0 :(得分:2)

在您的代码中,res是一个数组,其中一个对象具有多个使用相同名称(countrystate)的键,这是不可能的,所以我要去假设您的意思是多个对象,但是您忘记了{},(并假设情况属实),那么一个解决方案如下:

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"}]