【问题标题】:Group JSON by country and output a list of the states sorted alphabetically按国家分组 JSON 并输出按字母顺序排序的州列表
【发布时间】:2017-03-01 22:39:48
【问题描述】:

我想按国家/地区对输入的 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"
                }
            ]
        } 
      ]
}]; 

// 我尝试了以下递归函数,因为似乎可能有超过 1 个嵌套函数来获取信息。我试图 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)
}

【问题讨论】:

    标签: javascript json sorting recursion nested


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!!这个棒极了。输出数组是正确的,但是上面的排序功能不能正常工作,我还在努力。如果您知道任何其他排序方式,请告诉我。谢谢!
    • @user21 我想帮你,但我怕我不明白这个问题。在我提供的代码中,有一个排序函数被应用于output。然后有一个console.log 代表output,并在我执行它时得到了输出的注释。你是说你没有得到和我一样的输出,还是说我误解了它应该如何排序? (如果有,请说明要求)
    • 对不起,我必须为 sublime 中的键 a 输入错误。您的排序功能代码运行良好。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2016-08-01
    • 2018-09-15
    相关资源
    最近更新 更多