Collin McGuire
Collin McGuire

Reputation: 724

Iterating objects within javascript array

I am trying to get objects from an array. For example;

var array = [{foo:'bar', baz: 'quz'}];

I can access the objects as follows

array[0].foo
// Which would return 'bar'

But I want to be able to loop through and print all the objects. Is there anyway to do so? Is there anything similar to a wildcard like '*' to grab everything?

Upvotes: 1

Views: 32

Answers (1)

hsz
hsz

Reputation: 152206

Just loop your array:

for ( var i = 0; i < array.length; i++ ) {
  for ( var key in array[i] ) {
    var value = array[i][key];
  }
}

Upvotes: 5

Related Questions