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?
Definition and Usage. The for...in statements combo iterates (loops) over the properties of an object. The code block inside the loop is executed once for each property.
The only difference between them is the entities they iterate over: for..in iterates over all enumerable property keys of an object. for..of iterates over the values of an iterable object.
In the inner loop you are going to loop through each of the 3 inner arrays one at a time. for (var j=0; j < arr[i]. length; j++) { //Handle inner array. } So on your first time through the outer loop i=0 and arr[i] is going to equal [1,2] because you are grabbing the 0th element.
The classic way of doing this is as follows:
for(var i = 0; i < nameArray.length; i++){
var str = nameArray[i];
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With