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 the scope object (optional) running 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() runs a given function on each item in the array, and if each item in the array returns true, it returns true.
some() runs a given function on each item in the array, and if any item of the array returns true, it returns true.
fliter() returns true if each item of the array returns true. Returning an array of items that will return true.
map() returns true if each item of the array returns 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 = 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()numbers.forEach(function(item, index, array){ alert(item);}); // Multiple pop-up windows display elements in the array separatelyAnother 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 String.prototype.toUpperCase.apply(elem);}var print = function(elem,index){console.log(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 = arr.every(isNum);console.log(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 = arr.some(isNum);console.log(res);//true/*Execute a test function on each item in the array and construct a new array. The items returning true are added to the new array. If an item returns false, the new array will not contain this item */res = arr.filter(isNum);console.log(res);//[3, 4, 5, 6, 7]/*Execute a function on each item in the array and construct a new array, and add the function knot of each item in the original array to the new array. */res = arr.map(toUpperCase);console.log(res);//["3", "4", "5", "6", "7", "A"]/*Execute function on each item in the array without returning the value*/res = arr.forEach(print);console.log(res); //Extend it yourself/*Array.prototype.every = function(fun,obj) {var len = this.length;if (typeof fun != "function")throw new TypeError();for (var i = 0; i < len; i++) {if (!fun.call(obj,this[i], i, this)) return false;} return true;};*/The above is all about this article. I hope it will be helpful for everyone to learn the JavaScript array iteration method.