Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...