Summarize six kinds of iterator _javascript tips in JavaScript

Source: Internet
Author: User

1.forEach iterators

The Foreach method receives a function as an argument, each element of the array uses this function, only this function is called, and there is no change in itself.

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 results of the output in the browser are:

2.every iterators

The Every method accepts a function that returns a Boolean type, which is used by each element in the array, and returns True if the function returns true for all elements, or false

Every iterator
function IsEven (num) {return
  num% 2 = 0;
}
var nums = [2,4,6,8];
document.write (Nums.every (IsEven));

3.some iterators

The Some method also accepts a function that returns a Boolean type, and returns True if one element makes the function return True

Some iterator
function IsEven (num) {return
  num% 2 = 0;
}
var nums = [1,3,5,7];
document.write (Nums.some (IsEven));

4.reduce iterators

The reduce method takes a function that returns a value that starts with an accumulation value and calls the function on successive elements in the cumulative value and array, knowing the last element in the list and finally getting the cumulative value returned

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 results obtained are: 55

reduce()Functions and add() functions together, from left to right, once the elements in the array, the execution process is as follows:

Add (1,2)-> 3
Add (3,3)-> 6
Add (6,4)-> Add (10,5)-> Add (
15,6)
->
Add ( 21,7)->
Add (28,8)->
Add (36,9)-> Add (45,10
)-> 55

reduceMethod can also be used to link elements in an array into a long string, as follows

Use reduce to connect array element
function concat (accumulatedstring, item) {return
  accumulatedstring + item;
}
var words = [' the ', ' quick ', ' brown ', ' Fox '];
var sentence = Words.reduce (concat);
document.write (sentence);

The final output results are as follows:

JavaScript also provides reduceRight methods, Reduce Unlike methods, which are executed from right to left, as follows:

Use reduce to connect array element
function concat (accumulatedstring, item) {return
  accumulatedstring + item;
}
var words = [' the ', ' quick ', ' brown ', ' Fox '];
var sentence = Words.reduceright (concat);
document.write (sentence);

The results of the implementation are as follows:


5.map iterators

The map iterator is similar to foreach, but the map changes the array, generates a new array, and the following code

Generates a new array
function curve (grade) {return
  grade+5
}
using the map iterator var grades = [77,65,81,92,83];
var newgrades = Grades.map (curve);
document.write (Newgrades);

Output results:

6.fiter iterators

Like a every iterator, passing in a function that returns a Boolean type, and the every method differs, when all the elements in an array are true for the result returned by the function, the method does not return true, but instead returns a new array. The array contains the elements that the corresponding function returns the result to be true, and the following code

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 results are as follows:

Summarize

The above is a summary of six kinds of iterators in JavaScript, I hope the content of this article will help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.