2016-10-20 1 views
-1

Ich möchte den JSON-Input nach Ländern gruppieren und eine alphabetisch sortierte Liste von Ländern ausgeben und die Namen der Staaten in jedem Land alphabetisch auflisten. könnte sein, mehr als 1 verschachtelte Funktion für InformationenGruppiere JSON nach Land und gebe eine alphabetisch sortierte Liste der Bundesländer aus

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

// Ich habe die folgende rekursive Funktion versucht, weil es, dass es zu sein scheint. Ich habe versucht, die erwartete Ausgabe console.log, aber es das Ergebnis nicht ausgegeben, die ich mag:

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

Antwort

1

in Ihrem Code res ist ein Array mit einem Objekt mit mehreren Schlüsseln der gleichen Namen verwenden (country und state), was nicht möglich ist, so werde ich dich bestimmt, mehrere Objekte zu übernehmen, aber sie vergessen, die { und } s, (und unter der Annahme, dass der Fall ist,) dann eine Lösung wäre folgende:

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

Danke !! Es ist brilliant. Das Ausgabe-Array ist korrekt, aber die obige Sortierfunktion funktioniert nicht richtig, ich arbeite immer noch daran. Bitte lassen Sie es mich wissen, wenn Sie eine andere Möglichkeit kennen, es zu sortieren. Vielen Dank! – user21

+0

@ user21 Ich möchte Ihnen helfen, aber ich fürchte, ich verstehe das Problem nicht. In dem Code, den ich bereitgestellt habe, gibt es eine Sortierfunktion, die auf "Ausgabe" angewendet wird. Und dann gibt es eine 'console.log' für' output', und einen Kommentar mit der Ausgabe, die ich bekommen habe, als ich sie ausgeführt habe. Willst du sagen, dass du nicht die gleiche Ausgabe wie ich bekommst, oder sagst du, dass ich falsch verstanden habe, wie es sortiert werden sollte? (Wenn ja, bitte spezifizieren Sie die Anforderungen) – StubbornShowaGuy

+0

Sorry ich muss falsch für den Schlüssel a in erhaben geben. Ihr Code für die Sortierfunktion funktioniert gut. Vielen Dank für deine Hilfe! – user21

Verwandte Themen