1.forEach iterator
The forEach method receives a function as a parameter, uses this function for each element in the array, and only calls this function, the array itself does not change any
//forEach iterator function square(num){ document.write(num + ' ' + num*num + '<br>');}var nums = [1,2,3,4,5,6,7,8];nums.forEach(square);The result output in the browser is:
2.every iterator
Every method accepts a function with a Boolean return value, and uses this function for each element in the array. If the function returns true for all elements, the method returns true, otherwise it returns false
//every iterator function isEven(num){ return num % 2 == 0;}var nums = [2,4,6,8];document.write(nums.every(isEven));3.some iterator
Some method also accepts a function with a Boolean return value. As long as there is an element that causes the function to return true, the method returns true.
//some iterator function isEven(num){ return num % 2 == 0;}var nums = [1,3,5,7];document.write(nums.some(isEven));4. Reduce iterator
Reduce method accepts a function and returns a value. The method starts with an accumulated value, constantly calls the function on the accumulated value and subsequent elements in the array, knows the last element in the array, and finally gets the returned accumulated value
//reduce iterator function add(runningTotal, currentValue){ return runningTotal + currentValue;}var nums = [1,2,3,4,5,6,7,8,9,10];var sum = nums.reduce(add);document.write(sum);The result is: 55
Together with the reduce() add() , the elements in the array are summed from left to right. The execution process is as follows:
add(1,2) -> 3add(3,3) -> 6add(6,4) -> 10add(10,5) -> 15add(15,6) -> 21add(21,7) -> 28add(28,8) -> 36add(36,9) -> 45add(45,10) -> 55
reduce method can also be used to link elements in an array into a long string. The code is as follows
//Use reduce to concatenate array elements function concat(accumulatedString, item){ return accumulatedString + item;}var words = ['the ', 'quick ', 'brown ', 'fox'];var sentence = words.reduce(concat);document.write(sentence);The final output result is as follows:
JavaScript also provides a reduceRight method, which is executed from right to left, unlike Reduce method, as follows:
//Use reduce to concatenate array elements function concat(accumulatedString, item){ return accumulatedString + item;}var words = ['the ', 'quick ', 'brown ', 'fox '];var sentence = words.reduceRight(concat);document.write(sentence);The execution results are as follows:
5.map iterator
Map iterator is somewhat similar to forEach, but map will change the array and generate a new array, as shown in the following code
//Use map iterator to generate a new array function curve(grade){ return grade+5;}var grades = [77,65,81,92,83];var newgrades = grades.map(curve);document.write(newgrades);Output result:
6.fiter iterator
Similar to the every iterator, passing in a function with a Boolean return value. Unlike the every method, when all elements in the array return the result of the corresponding function is true, the method does not return true, but returns a new array, which contains the elements with the corresponding function returning the result of true. The code is as follows
function isEven(num){ return num % 2 == 0;}function isOdd(num){ return num % 2 != 0;}var nums = [];for (var i=0; i<20; i++) { nums[i] = i+1;}var evens = nums.filter(isEven);document.write(evens);document.write('<br>');var odds = nums.filter(isOdd);document.write(odds);The output result is as follows:
Summarize
The above is a summary of six iterators in JavaScript. I hope the content of this article will be helpful to everyone's study and work.