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?

Community
  • 1
  • 1
eric
  • 255
  • 1
  • 2
  • 6
  • 8
    This is an object, not an array. – Felix Kling Sep 10 '12 at 02: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 '12 at 02:32
  • Strictly, it's an [Object intialiser](http://ecma-international.org/ecma-262/5.1/#sec-11.1.5), not an [Array initialiser](http://ecma-international.org/ecma-262/5.1/#sec-11.1.4). – RobG Sep 10 '12 at 02:35
  • 4
    @slebetman—technically (i.e. according to the relevant standard, [ECMA-262](http://ecma-international.org/ecma-262/5.1/)) 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 '12 at 02:38

2 Answers2

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
    }
}
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • 3
    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 '20 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.

phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
  • 1
    It's `for-in` not `in`. They're two different things in JS. – gray state is coming Sep 10 '12 at 02:24
  • I don't understand what you mean? – phenomnomnominal Sep 10 '12 at 02: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. – gray state is coming Sep 10 '12 at 02: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 '12 at 02:33
  • @graystateiscoming: He's still right when he says use `in`. He's not saying to use an in-loop because such a thing doesn't exist. He's saying use `in` in your `for` loop. – slebetman Sep 10 '12 at 02:34
  • 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++) {`. – gray state is coming Sep 10 '12 at 02:38
  • Both are valid points. I've edited to clarify. – phenomnomnominal Sep 10 '12 at 02:39