Summary: six types of iterators in javascript and six types of javascript
1. forEach iterator
The forEach method receives a function as a parameter and uses this function for each element in the array. Only this function is called, and the array does not change.
// ForEach iterator function square (num) {document. write (num + ''+ num * num + '<br>');} var nums = [1, 2, 4, 5, 6, 7, 8]; nums. forEach (square );
The output result in the browser is:
2. every iterator
The every method accepts a function whose return value is boolean. This function is used for each element in the array. If the function returns true for all elements, this method returns true, otherwise, false is returned.
// Function isEven (num) {return num % 2 = 0;} var nums = [2, 4, 6, 8]; document. write (nums. every (isEven ));
3. some iterators
Some methods also accept a function whose return value is boolean. If an element causes the function to return true, this 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
The reduce method accepts a function and returns a value. This method starts from an accumulated value and constantly calls this function for the accumulated value and subsequent elements in the array to know the last element in the array, finally, the accumulated value is returned.
// Reduce iterator function add (runningTotal, currentValue) {return runningTotal + currentValue;} var nums = [1, 2, 4, 5, 6, 7, 8, 9, 10]; var sum = nums. reduce (add); document. write (sum );
The result is: 55.
reduce()
Functions andadd()
Function together, from left to right, a sum of elements in the array, 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
The method can also be used to link elements in the array into a long string. The Code is as follows:
// Use reduce to connect to the array element function concat (accumulatedString, item) {return accumulatedString + item;} var words = [''the, 'quick', 'Brown ', 'fox']; var sentence = words. reduce (concat); document. write (sentence );
The output result is as follows:
Javascript also providesreduceRight
Method, andReduce
The method is different. It is executed from right to left, as shown below:
// Use reduce to connect to the array element function concat (accumulatedString, item) {return accumulatedString + item;} var words = [''the, 'quick', 'Brown ', 'fox']; var sentence = words. reduceRight (concat); document. write (sentence );
The execution result is as follows:
5. map iterator
The map iterator is similar to forEach, but map will change the array and generate a new array. The following code:
// Use the map iterator to generate a new Array function curve (grade) {return grade + 5;} var grades = [, 83]; var newgrades = grades. map (curve); document. write (newgrades );
Output result:
6. fiter iterator
Similar to the every iterator, a function with a Boolean return value is passed in, andevery
The method is different. When all element pairs in the array are true, this method does not return true, but returns a new array, the array contains the elements whose return result is 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:
Summary
The above is a summary of the six iterators in javascript. I hope the content of this article will be helpful for your learning.