How to loop on an array of objects using for in?


#1

How to use for in and print every properties of array using for in?

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5
};

var nyx = {
    fullName: "New York Cityawiaidha",
    mayor: "Bill de Blasioaowjaowjd",
    population: 80000008484,
    boroughs: 5
};

var array= [ nyc, nyx ];

for( var property in array )
{
    console.log( array[property] ); // this doesnt work i just want to loop the nyc and nyx 
}

#2

you can't, you stored the objects into the the array, take a look:

console.log(array)

see? Array contains the objects, so what you want to do is impossible


#3

@kevinmogi,

========= looping over an array ===

The most common way to loop over all elements of an Array
is using the generic FOR-loop
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

for ([initialization]; [condition]; [final-expression]) {
      statement
}

You would
start at index zero,
continue as long as the index is =less than= the array.length,
and at each iteration increment / decrement the index-pointer

var myArray =["a",2,{anObjectKey: "anObjectValue"}];
for ( var index = 0, len = myArray.length; index < len ; index= index + 1 ) {
     // get access to the =element-Value= using the [bracket-notation]
     console.log( index + "==> "+myArray[index] );
}

== Do NOT use FOR-IN loop on an Array ==

You could look at an Array as it being an Object like

 myArray = {
      0: "a",
      1: 2,
      2: { anObjectKey: "anObjectValue"}
 }

and with the FOR-IN loop

for (var key in myArray) {
     //getting access to the =associate property-Value= 
      //using the =key= in a [bracket-notation]
      console.log( key + "==>"+myArray[key] );
}

but do a google search
== discussions / opinions ==
javascript why is it a bad idea to use for-in on an Array site:stackoverflow.com
for instance
= http://stackoverflow.com/questions/5269757/why-is-javascripts-for-in-loop-not-recommended-for-arrays
[quote]
An array is an object,
and array elements are simply properties with the numeric index converted to string.
For example, arr[123] refers to a property "123" in the array object arr.

The for ... in construct
works on all objects, not just arrays,
and that is cause for confusion.

When somebody for ... in an array,
most often the programmer intends to iterate just all the items, even most likely in order.

The semantics is so similar to array iteration in other programming languages that it is very easy to get confused.
In JavaScript, this construct does not iterate array elements in order.
It iterates all

  • the array's properties
  • (including inherited prototype functions, any properties added to it, any other non-element properties added to it etc.),
  • and not in order at all.
  • It will even find the property "length".

This is most likely not what the programmer means, if he/she comes from another programming language.

That's why you shouldn't do it.
You shouldn't use a language construct
that looks like it does obvious things, but actually does things that are completely different.
It creates bugs that are very obscure and very difficult to find.
[end quote Stephen Chung]


#4

for(var property in nyc){
console.log(property);
}

It doesn't want the value's of the properties, it just wants the properties itself. To attain the values, try:

for(var property in nyc){
console.log(nyc[property]);
}


#5