var nameArray = [
{ name: 'john', surname: 'smith' },
{ name: 'paul', surname: 'jones' },
{ name: 'timi', surname: 'abel' },
];
for (str of nameArray) {
console.log( str.name );
}
I want to know, how supported is for( item of array )
in terms of browser support, mobile JavaScript support - I realize you cannot do greater than >
and this is pure iteration?
I have just discovered this, is this as good as I hope it is?
In the meantime, you could use something like this:
for...in
has been standard since ECMAScript 1st Edition.The classic way of doing this is as follows:
This will give you the exact functionality of a "foreach" loop, which I suspect is what you're really after here. This also gives you the added benefit of working in Internet Explorer.
There is also extensive knowledge of the exact loop described in the MDN. At this time Android web and it seems not everything supports your method so check the compatibility list on that page; seems to be a future release of the new JavaScript that will probably have OOP inside it.
This is the ES6
for..of
loop. According to the MDN article i just linked, it's supported by several browsers (see there for exact versions), but not IE. Currently, several mobile browsers also support it.MDN:
The above is what
for...of
loop does. The below is its current status.It's not.
Even if it were, it would be inappropriate to use it on an array.
For arrays, you should always use a traditional
for
loop.You can, however, spice it up a bit: