0

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

oussama kamal
  • 1,027
  • 2
  • 20
  • 44
  • Note, [it's not generally recommended to use `for..in` loops with Arrays](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea). – Jonathan Lonowski Mar 06 '16 at 16:15
  • Can someone stick with my code please and add what is necessary to access the state_code property? I don't want a new solution but a fix for mine.Thanks – oussama kamal Mar 06 '16 at 16:20
  • just put it in the dev console (f12 on chrome) with "debugger;" line before it. by debugging it you will understand the problem in seconds. – Ronen Ness Mar 06 '16 at 16:24
  • @oussamakamal The short answer is: "You'll need a 2nd loop – `data[key1].state[key2].state_code`, etc." Beyond that, the answers being given are suggesting fixes, they're just also suggesting better practices. – Jonathan Lonowski Mar 06 '16 at 16:30

6 Answers6

1

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

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

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)
        }
    };
};
Shayan
  • 956
  • 9
  • 20
0

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

cl3m
  • 2,791
  • 19
  • 21
0

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);
   }
 }
  
});
user2950720
  • 931
  • 2
  • 10
  • 26
0

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);
        }
    }
}
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

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);
        }
    }
Ahsan Iqbal
  • 78
  • 1
  • 4
  • 10