ECMAScript5 defines 5 iterative methods for arrays. Each method receives two parameters: the function to run on each item and the scope object (optional) that runs the function (i.e. the value that affects this). The functions passed into these methods receive three parameters: the value of the array item, the position of the item in the array, and the array object itself. Depending on the usage method, the return value after execution of this function may or may not affect the return value of the method. These 5 iterative methods are:
All five iterative methods accept two parameters: the function to run on each item and the scope of the function running (optional)
every(): Run the given function on each item in the array. If the function returns true for each item, it returns true.
filter(): Runs the given function on each item in the array. Returning an array of items that will return true.
forEach(): Runs the given function for each item in the array. The function does not return a value.
map(): Run the given function on each item in the array. Returns the function composed of the result of each function call.
some(): Run the given function on each item in the array. If the function returns true for either, it returns true
All of the above methods will not modify the values contained in the array.
In the above method, every() and some() are very similar, both used to query whether an item in the array satisfies a certain condition. For the every() method, the passed function must return true for each item before this method returns true. Otherwise, it returns false. The some() method returns true as long as the passed function returns true to an item in the array. For example:
var nums = [1,2,3,4,5,4,3,2,1];var result = nums.every(function(item, index, array){ return (item > 2);})console.info(result);The above code will print false in the console.
var nums = [1,2,3,4,5,4,3,2,1];var result = nums.some(function(item, index, array){ return (item > 2);})console.info(result);The above code prints true in the console.
The following is an example of the filter() function, which uses the specified function to determine whether there is an item in the returned array. For example, to return an array with all values greater than 2, you can use the following code:
var nums = [1,2,3,4,5,4,3,2,1];var result = nums.filter(function(item, index, array){ return (item > 2);})console.info(result); // [3,4,5,4,3]The above code returns an array containing 3, 4, 5, 4, 3 by calling the filter() method. This method is very useful for querying all arrays that meet certain criteria.
The map() method also returns an array, and each item in this array is the result of running the incoming function on the corresponding item in the original array. For example, you can multiply each item in the array by 2 and then return an array of these products:
var nums = [1,2,3,4,5,4,3,2,1];var result = nums.map(function(item, index, array){ return item * 2;})console.info(result); // [2,4,6,8,10,8,6,4,2]The map() method is suitable for creating an array containing items one by one in another array.
The last one is the forEach() method, which just runs the passed function on every item in the array. This method has no return value, essentially iterating over an array using a for loop. See the following example:
var nums = [1,2,3,4,5,4,3,2,1];nums.forEach(function(item, index, array){ //Execute the required operation})These array methods in js can greatly facilitate the processing of array tasks by performing different operations.
Browsers that support these iterative methods are: IE9+, Firefox2+, Safari3+, Opera9.5+ and Chrome.
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; //every() and some() are 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 is essentially no difference from for loop var forEachResult=numbers.forEach(function(item,index,array){ alert(item) });The above is all about this article. I hope to give you some tips to better understand the JavaScript iteration method.