Elementary Introduction to JavaScript Iterative method _ Basic knowledge

Source: Internet
Author: User

Five iterative methods accept two parameters: the function to run on each item and the scope to run the function (optional)

Every (): Runs the given function for each item in the array. Returns True if the function returns true for each item.
Filter (): Runs the given function for each item in the array. Returns an array of items that the function returns True.
ForEach (): Runs the given function for each item in the array. The function does not have a return value.
Map (): Runs the given function for each item in the array. Returns a function that consists of the results of each function call.
Some (): Runs the given function for each item in the array. Returns True if the function returns true for either item

Copy Code code as follows:

var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Every () and some () most similar
Every () Item: Current traversal item, index: Current item index, array: Array object itself
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 has no difference in nature from the For loop
var Foreachresult=numbers.foreach (function (Item,index,array) {
Alert (item)
});

The above is the entire content of this article, I hope to give you some hints, to better understand the JavaScript iterative method.

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.