JS Array Traversal some,foreach,map,filter,every comparison

Source: Internet
Author: User

1. [...]. True if the Some (CK) function----One of the true

Performs a CK function once for each element in the array, and returns True if an element returns TRUE. False if both return false

Checks whether the entire array has elements that satisfy the CK function.

1. var result = [1,5,3,6].some (  (v,i) = (  v>10))      // all elements are not satisfied, return result = False2,var result = [10,5,30,60].some (  v,i) =  (v<10))      / / have one (multiple) satisfied, return result  = True
2. [...]. The foreach (CK) function----loop.

Each array element executes the CK function once, and the foreach function cannot jump out of the

1 [49].foreach, (v,i) = Console.log (v));    // about      
3. [...]. The map (CK) function----Returns a collection of return values for each element

Each array element executes the CK function once, and finally returns a collection of values returned each time the element executes the CK function (array)

1 var newarray = [50,30,40].map ((v,i) = V/10)       //Each element is divided by 10 and finally returns a new array newarray = [5,3,4]
4. [...]. The filter (CK) function----Get a collection of elements that return a value of true

Each array element executes the CK function once, and finally returns a collection of elements (arrays) of true for the return value after each element's execution of the CK function

var newarray = [50,2,60,4,53,15].filter ((v,i) = (v>10))   // returns a new array  of elements greater than 10 in the array NewArray = [50,60,53,15]
5. [...]. Every (CK) function----One of false, returns false

Each array element executes the CK function once until an element executes the function CK returns FALSE, and returns True if all returns True.

1 var result = [5,50,35,12,85].every ((v,i) + v<51)  // return with one (multiple) greater than or equal to 51, then return result = False  2var result = [5,50,35,12,85].every ((v,i) = v<100)  // all greater than 51, returns result = True

6. [...]. Reduce (ck,init)----perform CK (prv.next) in turn

The array executes the CK function in turn.

1 var result = [0,1,2,3]2 restult.reduce ((A, b) = a+b,0)  //  return 6

JS Array Traversal some,foreach,map,filter,every comparison

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.