24

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

If I have a javascript object like this :

data = {
    a : 2,
    b : 3
}

but a and b are arbitrary and decided at runtime. Is there any way to go through the object and access all properties without knowing the key?

4
  • 8
    This is an object, not an array. Sep 10, 2012 at 2:18
  • 1
    @FelixKling: Technically, in computer science, a javascript object emulates a data structure known as an associative array. Which is regarded as a kind of array. So it is technically correct to call it an array because it emulates a type of array.
    – slebetman
    Sep 10, 2012 at 2:32
  • Strictly, it's an Object intialiser, not an Array initialiser.
    – RobG
    Sep 10, 2012 at 2:35
  • 4
    @slebetman—technically (i.e. according to the relevant standard, ECMA-262) there are no associative arrays in javascript. Objects are just unordered collections of name/value pairs. Arrays are just Objects with a special length property, some handy methods that can be applied generally to any suitable object, even host objects in some cases, and a different initiliser.
    – RobG
    Sep 10, 2012 at 2:38

2 Answers 2

35
data = {
    a : 2,
    b : 3
}

for(var propName in data) {
    if(data.hasOwnProperty(propName)) {
        var propValue = data[propName];
        // do something with each element here
    }
}
1
  • 4
    This is an old question but, as of today, you can get the values of an object as an array using Object.values(data)
    – Joe Maffei
    Jul 17, 2020 at 19:23
3

Firstly, that isn't what is commonly known in JS as an Array, it's normally known as an Object. Arrays just contain values i.e.

arr = [1, 2, 3, 4]

Whereas Objects ('Associative arrays') associate name: value pairs.

To iterate over the values of an Object, use for...in

var object = { a: 'hello' }

for (var key in object) {
  if (object.hasOwnProperty(key)) {
    alert(key); // 'a'
    alert(object[key]); // 'hello'
  }
}   

The hasOwnProperty is important, to ensure you are only looking at the actual object, and not properties that belong to the prototype.

7
  • 1
    It's for-in not in. They're two different things in JS. Sep 10, 2012 at 2:24
  • I don't understand what you mean? Sep 10, 2012 at 2:31
  • Your code is right, but the sentence above it says to use in. In JavaScript, in is an operator that is distinct from the for-in enumeration statement. Sep 10, 2012 at 2:32
  • Note that Arrays are Objects that use a different syntax for their initialiser (aka string literal representation). Arrays just have a special length property and some handy inherited methods that can be generically applied to other Objects.
    – RobG
    Sep 10, 2012 at 2:33
  • 1
    @slebetman: For what for loop? A for statement is also distinct from a for-in statement. If we're talking in JavaScript syntax, it doesn't make sense to say "just use in in your for loop" unless you're talking about something like for(i = 0; i in arr; i++) {. Sep 10, 2012 at 2:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.