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

4
  • Note, it's not generally recommended to use for..in loops with Arrays. Commented Mar 6, 2016 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 Commented Mar 6, 2016 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. Commented Mar 6, 2016 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. Commented Mar 6, 2016 at 16:30

6 Answers 6

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)
        }
    };
};
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.