Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How well is the `for of` JavaScript statement supported?

Tags:

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?

like image 927
TheBlackBenzKid Avatar asked Dec 09 '14 16:12

TheBlackBenzKid


People also ask

What is a for statement in JavaScript?

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.

What is difference between Forin and for OF in JavaScript?

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.

How can use two for loop in JavaScript?

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.


1 Answers

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.

like image 124
Gianthra Avatar answered Oct 09 '22 01:10

Gianthra