An iterative approach to arrays

Source: Internet
Author: User

One, every (), runs the given function for each item in the array, and returns True if the function returns true for each item.

var arr=[1,2,3,4,5,6,7,8,9];everyresult=arr.every (function(item,index,array) {     return (item>2);    

The result of execution is false;

var arr=[1,2,3,4,5,6,7,8,9];arr.foreach (function(item,index,array) {     // perform certain Actions })

Two, filter (); Each item in an array runs the given function, and returns the list of items that the function returns True.

var Arr=[1,2,3,4,5,6,7,8,9];var filterresult=arr.filter (function (item,index,array) {    return (item>2);}) Alert (Filterresult ());

Returns an array and each value in the array is greater than 2 [3,4,5,6,7,8,9];

Three, ForEach (); Runs the given function for each item in the array, and the method does not return a value; it is essentially the same as using a for loop to iterate over an algebraic group.

var arr=[1,2,3,4,5,6,7];arr.foreach (function(item,index,array) {    //  perform certain Actions })

Four, map, an array of each of the arrays that run the given function, nearly returning the result of each function call.

var arr=[1,2,3,4,5,6,7]; var mapresult=arr.map (function(item,index,array) {    return item*2;}); Alert (Mapresult)

The value returned is; [2,3,6,8,10,12,14];

Five, some, runs the given function for each item in the array, and returns True if the function returns true for either item.

var arr=[1,2,3,4,5,6,7]; var someresult=arr.some (function(item,index,array) {    return ( Item>2);}); alert (someresult);

The returned result is true;

Iterative methods for arrays

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.