Array iteration method in javascript-example analysis-javascript skills

Source: Internet
Author: User
This article mainly introduces the Array iteration method in javascript. The example analyzes the definition and usage skills of the Array iteration method, for more information about javascript iteration, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:

The code is as follows:

// Filter () uses the specified function to determine whether to return an included item in the array
Var num = [1, 2, 3, 4, 5, 6, 12];

Num. filter (function (item, index, array ){
Return (item> 2); // [3, 4, 5, 6, 12]
});

// Map () returns an array. each item in the array is the result of running the input parameter on the corresponding item in the original array.

Var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Num. map (function (item, index, array ){
Return (item * 2); // [2, 4, 6, 8, 10, 8, 6, 4, 2]
});

// Every () some (): query whether an item in the array meets a condition. if every parameter required by every () is returned, true is returned. some () method
// If one of them is true, the result is true.

Var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Num. every (function (item, index, array ){
Return (item> 2); // false
});

Num. some (function (item, index, array ){
Return (item> 2); // true
})

// ForEach () is used to input parameters for each array. no return value is returned.
Var num = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Num. forEach (function (item, index, array ){
Return item;
})

I hope this article will help you design javascript programs.

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.