user21 user21 - 1 month ago 5
JSON Question

Group JSON by country and output a list of the states sorted alphabetically

I would like to group the input JSON by country and output a list of countries sorted alphabetically and list the names of the states alphabetically in each country.

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


// I have tried the following recursive function because it seems that there might be more than 1 nested function for information. I tried to console.log the expected output, but it does not output the result which I would like:

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)
}

Answer

In your code, res is an array with one object with multiple keys using the same names(country and state), which isn't possible, so I'm going to assume you meant multiple objects but you forgot the { and }s, (and assuming that's the case, ) then one solution would be the following:

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