0

Possible Duplicate:
How do I enumerate the properties of a javascript object?

I have an object with keys like this:

var OBJ = {
 "Some value": {val1: 53, val2: 43},
 "Another one": {val1: 35, val2: 41},
 "One More": {val1:32, val2: 43}
};

I want to traverse through it with a for loop, and use the val1 and val2 values. If this was an array, I'd just do this:

for(var i = 0; i < VAR.length; i++){
   VAR[i].val1;
}

But how do I do this with an object that doesn't have a numberic key?

Thanks!

Community
  • 1
  • 1
swickblade
  • 4,506
  • 5
  • 21
  • 24
  • 3
    possible duplicate of [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object), http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object – Matt Nov 07 '11 at 16:19
  • `for(var i in OBJ)`, going to look for duplicates now.. – Rob W Nov 07 '11 at 16:19

1 Answers1

4

Try the following

for (var name in VAR) {
  if (VAR.hasOwnProperty(name)) {
    VAR[name].val1;
  }
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454