0

Here are many examples how to iterate over javascript Objects. This works actually fine for me

    var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';

obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;

But didn't work out how to iterate over the values in obj. Tried like this

    for(var k in obj) {
    if (obj.hasOwnProperty(k)) {
        if (obj.hasOwnProperty(k)) {
             out +=("Key is " + k + ", value is" + obj[k].firstName);
        }
    }
}

but failed. As you can see stolen from examples. If someone can help with some javascript code to iterate over the JSON key-value pairs ?

user3732793
  • 1,699
  • 4
  • 24
  • 53
  • 2
    Why did you use `if (obj.hasOwnProperty(k)) {` twice? – Cerbrus Aug 18 '15 at 13:55
  • 1
    Why are you writing `text` out a string then parsing it instead of just declaring as an object in the first place, which would be much easier programatically and semantically? – Popnoodles Aug 18 '15 at 13:56
  • 2
    @Popnoodles: I would assume to emulate receiving it from an ajax call or similar (but it's an assumption). – T.J. Crowder Aug 18 '15 at 13:59
  • Cerbrus this was just a trie to iterare over both key and values. Popnoodles...because I don't know js good enough....anyhow do you have an example how to iterate such a json array ? – user3732793 Aug 18 '15 at 14:06

3 Answers3

2

Once parsed, your obj variable refers to an object with an employees property which refers to an array. So:

obj.employees.forEach(function(employee) {
    // ...
});

or

var i, employee;
for (i = 0; i < obj.employees.length; ++i) {
    employee = obj.employees[i];
    // ...
}

...or any of the several other ways outlined in this answer.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Your example json has parent key employees, which has child array of objects. Then you have to loop through array if you know the key else you can use Object.keys which will return list of keys in object in array format.

var text = '{"employees":[' +
  '{"firstName":"John","lastName":"Doe" },' +
  '{"firstName":"Anna","lastName":"Smith" },' +
  '{"firstName":"Peter","lastName":"Jones" }]}';

obj = JSON.parse(text);

Object.keys(obj).forEach(function(k) { //Loop through keys of obj.
  obj[k].forEach(function(elm) {  //Loop through all elements of first object array
    console.log(elm.firstName+"_"+elm.lastName)
  })
})
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
0

It's not working as you are trying to access obj["employees"].firstName which does not exist.

for(var k in obj) {
  if (obj.hasOwnProperty(k)){
    var employee = obj[k];
    for(var i in employee){
      out +="Key is " + i + ", value is" + employee[i].firstName;
    }
  }
}

OR

var employee = obj["employees"];
for(var i in employee){
  out +="Key is " + i + ", value is" + employee[i].firstName;
}
Xlander
  • 1,331
  • 12
  • 24