简体   繁体   中英

Read complex JSON in Javascript

I have a json like :

var data = [
   {
      "country":"Andorra",
      "code":"AD",
      "state":[
         {
            "state_code":"AD1",
            "state_description":"aaAndorra1"
         },
         {
            "state_code":"AD2",
            "state_description":"aaAndorra2"
         }
      ]
   }
]

I would like to loop though the state property and get the state_code value

This is how I'm doing it :

for (var key in data) {
        if (data.hasOwnProperty(key)) {
            if(data[key].state.state_code === "AD1"){
                console.log(data[key].state.state_description);
        }
    }

I'm getting undefined.

Any help please?

Thanks

Try to iterate from the outer object and print it,

data.forEach(function(country){ //For the countries
  country.state.forEach(state){ //For the states
    console.log(state.state_code,state.state_description);
  });
});

As a side note, You should not use for-in loop while iterating over an array. Since it will iterate over all the enumerable properties of an object throughout the prototypes . I saw you using .hasOwnProperty() , that would help avoid such situations, but in our case, using for-in loop is unnecessary.

DEMO

try this code

for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].state.length; j++) {
        if(data[i].state[j].state_code === "AD1"){
            console.log(data[i].state[j].state_description)
        }
    };
};

If you want an array with every state_code:

var result = data.reduce(function(ar, d) {
    // iterate over the state array of each item
    d.state.forEach(function(s){
      // push the state_code property to our resulting array
      // you could add a condition such as 
      // if(s.state_code === "AD1") ar.push(s.state_code)
      ar.push(s.state_code)
    });
    return ar;
}, [])

See fiddle

 data.forEach(function(obj){ var states = obj.state; if(Array.isArray(states)){ states.forEach(function(state){ if(state.state_code === "AD1"){ console.log(state.state_description); } }); } else { if(states.state_code === "AD1"){ console.log(states.state_description); } } }); 

The for...in statement iterates over the enumerable properties of an object

For iterating Arrays use for loop or Array#forEach function

for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].state.length; j++) {
        if (data[i].state[j].state_code === "AD1") {
            console.log(data[i].state[j].state_description);
        }
    }
}

Your declare data variable is an array of object. Where each object contains an array of state in it. Hence, if you want to iterate into deep, loop should be something like this (it is working fine, i have tested).

    for (var i = 0; i <= data.length; i++) {
        for(var j = 0; j <= data[i].state.length; j++){
            alert("The code is ::" + data[i].state[j].state_code);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM