For Loop Array of Javascript Objects

var dog1 = {name:”frank”, breed:”mix”};

var dog2 = {name:”abby”, breed:”mix”};

var dogArray = [];

dogArray.push(dog1);

dogArray.push(dog2);

I have found some JavaScript engines are OK with this:

for(var x = 0; x < dogArray.length; x++){

But many are not, and it needs to be declared as:

for(var x = 0; x < Object.keys(dogArray).length; x++){

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: