Itérations en javascript
for (var i = 0; i < array.length; i++) {
}
break
or continue
or goto
to control your flow.map
var result = array.map(function(item) { });
.map
iterator is all about the fact that what you return from the function gets added to an array, and you should want that. Otherwise you're creating a big new array for no reason that's just filled with undefined
..reduce
var result = array.map(function(memo, item) { });
forEach
array.forEach(function(item) { });
.map
and .reduce
are functions that make it easier to transform data. You can use them as general-purpose "I want a loop" functions but it's not semantic and can be wasteful: if you just need a loop, use .forEach
or for
.
FAQ: