This article introduces the JavaScript array iteration method for your reference. The specific content is as follows
Each method receives two parameters: the function to run on each item and (optional) scope object that runs the function.
The functions passed into these methods will receive three parameters: the value of the array item, the position of the item in the array, and the array object itself.
forEach() Runs the given function on each item in the array. This method has no return value.
every() Running a given function, if each item in the array returns true, then true.
some() Running a given function for each item in the array, if any item in the array returns true, it returns true.
fliter() If each item of the array returns true, then true. Returning an array of items that will return true.
map() If each item of the array returns true, then true. Returns an array of results of each function call.
Please see the following example:
var numbers = [1,2,3,4,5,4,3,2,1]; //every() var everyResult = (function(item, index, array){ return (item > 2); }); alert(everyResult); //false //some() var someResult = (function(item, index, array){ return (item > 2); }); alert(someResult); //true //filter() var filterResult = (function(item, index, array){ return (item > 2); }); alert(filterResult); //[3,4,5,4,3] //map() var mapResult = (function(item, index, array){ return (item * 2); }); alert(mapResult); //[2,4,6,8,10,8,6,4,2] //forEach() (function(item, index, array){ alert(item); }); //Multiple pop-up windows display elements in the array separately
Another javascript array iteration method, as follows
var arr = [3,4,5,6,7,"a"]; var isNum = function(elem,index,AAA){ return !isNaN(elem); } var toUpperCase = function(elem){ return (elem); } var print = function(elem,index){ (index+"."+elem); } /*Execute the test function on each item in the array until the item that returns false for the specified function is obtained. Use this method to determine whether all items in the array meet a certain condition, similar to the meaning of &&*/ var res = (isNum); (res);//false; /*Execute the test function on each item in the array until the item returning true is obtained. Use this method to determine whether all items in the array meet the conditions. Similar to the meaning of ||*/ res = (isNum); (res);//true /*Execute the test function on each item in the array and construct a new array, and the item returning true is added to the new array. If an item returns false, this item will not be included in the new array*/ res = (isNum); (res);//[3, 4, 5, 6, 7] /*Execute a function on each item in the array and construct a new array, and add the function knots of each item in the original array into the new array. */ res = (toUpperCase); (res);//["3", "4", "5", "6", "7", "A"] /*Execute function on each item in the array without returning the value*/ res = (print); (res); //Extend it yourself /* = function(fun,obj) { var len = ; if (typeof fun != "function") throw new TypeError(); for (var i = 0; i < len; i++) { if (!(obj,this[i], i,this)) return false; } return true; };*/
The above is all about this article, and I hope it will be helpful for everyone to learn the iteration method of JavaScript array.