ECMASCRIPT5 defines 5 iterative methods for an array: every (), filter (), ForEach (), map (), some ()
Each method receives two parameters: the function to run on each item and, optionally, the scope object that runs the function-affects the value of this.
The functions passed in these methods receive three parameters: the value of the array item, the position of the item in the array, and the object itself.
Depending on the method used, the return value after the function is executed may or may not affect the return value of the method.
<! DOCTYPE html>varnumbers=[1,2,3,4,5,4,3,2,1]; //for every (), each item is true to return True varEveryresult=numbers.every (function(Item,index,array) {//The function passes in three parameters, the value of the array item, the position of the item in the array index, the array object itself array return(item>2); }); Console.log (Everyresult);//false //about some (), which has a true, returns True varSomeresult=numbers.some (function(Item,index,array) {return(item>2); }); Console.log (Someresult);//true //For filter (), returns an array of true elements for each of the function functions varFilterresult=numbers.filter (function(Item,index,array) {return(item>2); }); Console.log (Filterresult);//[3,4,5,4,3] //about Map (), returns an array of the results of a function call varMapresult1=numbers.map (function(Item,index,array) {returnItem*2; }); Console.log (MAPRESULT1);//[2,4,6,8,10,8,6,4,2] varMapresult2=numbers.map (function(Item,index,array) {return1; }); Console.log (MAPRESULT2);//[1,1,1,1,1,1,1,1,1] //with respect to foreach (), it is just the function passed in for each item in the array, which does not return a value, essentially the same as using a for loop to iterate over an algebraic groupNumbers.foreach (function(Item,index,array) {//perform certain actions, such asConsole.log (item);//The output is a loop each output value wraps the next value }); </script>An iterative method for elevation 5.2.8