Five iterative methods accept two parameters: the function to run on each item and the scope to run the function (optional)
Every (): Runs the given function for each item in the array. Returns True if the function returns true for each item.
Filter (): Runs the given function for each item in the array. Returns an array of items that the function returns True.
ForEach (): Runs the given function for each item in the array. The function does not have a return value.
Map (): Runs the given function for each item in the array. Returns a function that consists of the results of each function call.
Some (): Runs the given function for each item in the array. Returns True if the function returns true for either item
Copy Code code as follows:
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Every () and some () most similar
Every () Item: Current traversal item, index: Current item index, array: Array object itself
var everyresult = numbers.every (function (item, index, array) {
return item > 2;
});
alert (everyresult);//false
Some ()
var someresult = numbers.some (function (item, index, array) {
return item > 2;
});
alert (someresult);//true
Filter
var filterresult = numbers.filter (function (item, index, array) {
return item > 2;
});
alert (Filterresult);//[3,4,5,4,3]
Map ()
var mapresult = Numbers.map (function (item, index, array) {
Return (item * 2);
});
alert (Mapresult);//[2,4,6,8,10,8,6,4,2]
ForEach has no difference in nature from the For loop
var Foreachresult=numbers.foreach (function (Item,index,array) {
Alert (item)
});
The above is the entire content of this article, I hope to give you some hints, to better understand the JavaScript iterative method.