0

I'm trying to access a nested javascript object within a for loop. The first level objects show up just fine, but the second level children show up as undefined. However if I do this outside a loop, hard-coding the object access, it works just fine. What is going on?

This is the object I'm trying to access:

var groups = {
  "group1": {
    "sum": 25,
    "count": 5
  },
  "group3": {
    "sum": 5,
    "count": 5
  }
}

See what I mean here: https://jsfiddle.net/97o1jme2/

SoKeT
  • 590
  • 1
  • 7
  • 16

2 Answers2

2

You use for ... in

The for...in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

which gives you a variable with the key of the object, you are iterating on.

Then you need the right access to the object, instead of group["sum"] use the key with groups together.

groups[group]["sum"]
//^^^^^^^^^^^

or

groups[group].sum
//^^^^^^^^^^^

var groups = { group1: { sum: 25, count: 5 }, group3: { sum: 5, count: 5 } },
    group;

for (group in groups) {
  console.log("[Loop access] " + group + " - sum: " + groups[group].sum + " - count: " + groups[group].count);
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thank you, that works like a charm. I would like to understand why this is happening though. I would assume the loop variable would be of type Object, why is it a String? – SoKeT Feb 21 '17 at 14:40
  • because you are iteratin over the keys of the object. – Nina Scholz Feb 21 '17 at 14:42
  • 1
    That `for..in` is **bad** ... Use `for..of` instead ... – KarelG Feb 21 '17 at 14:46
  • @KarelG can you please explain why is for..in bad? I am facing a similar issue but find nothing to read ono – Asma Rahim Ali Jafri Oct 06 '21 at 07:13
  • 1
    @AsmaRahimAliJafri the [`for..of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) MDN documentation has [a section](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of#difference_between_for...of_and_for...in) about its difference towards `for..in`. Basically, the latter goes through all enumerable properties where as the former won't. It is important to know the differences between each other. I consider using `for..in` as a statement for exceptional use cases. – KarelG Nov 18 '21 at 11:40
1

If you perform console.log(group) in the loop you will see that at every iteration the group variable is not your desired object but it is a simple string - key of groups object. You would then access the proper object via groups[group] inside the loop.

EDIT

In order to be sure that you will obtain proper result I recommend you perform some validation inside the loop at every iteration

if ( groups.hasOwnProperty(group) ) {
    // perform operation
}

According to the documentation

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property.

piotrbienias
  • 7,201
  • 1
  • 28
  • 33