Array manipulation of array APIs (filter, map, some, every, sort)

Source: Internet
Author: User

var arr = [1,2,3,4];
ForEach
Arr.foreach ((Item,index,arr) = {    Console.log (item)//result is 1,2,3,4})
Filter
Filter filters out values in the array that do not meet the criteria, returns a new array, and does not change the value of the original array. var c=arr.filter ((Item,index,arr) = {return item > 2//new array for [3,4] filters the item that satisfies the condition returns a new array})
Map
var d=arr.map ((Item,index,arr) =>{return item*3;//walk through each item to return})
Some
var e=arr.some ((Item,index,arr) =>{return item>3;//returns True if satisfied, terminates the loop});
Every
var f=arr.every ((Item,index,arr) = {return item > 0//result is false})//Iterate through the array for each item, each item returns TRUE, and the final result is true. When any one of the items returns false, stop the traversal and return false. Does not change the original array console.log (C,D,E);
Sort  For two elements x and Y, if you thinkx < y, then return-1 if it is consideredx = = y, 0 is returned if it is consideredx > Y, it returns the results that 1//looks normal: [' Google ', ' apple ', ' Microsoft '].sort ();//[' Apple ', ' Google ', ' Microsoft '];//Apple ranked last: [' Google ', ' Apple ', ' Microsoft '].sort ();//[' Google ', ' Microsoft ', ' apple ']//incomprehensible results: [Ten, 1, 2].sort ();//[1, 10, 2, 20] according to the correspondingASCIICode to sort, while the lowercase letter AASCIIAfter the code is in capital letters but sort is also a higher-order function that can be customized by receiving a comparison function to implement a custom collation
var arr = [' Google ', ' apple ', ' Microsoft '];arr.sort (function (s1, s2) {    x1 = s1.touppercase ();    x2 = S2.touppercase ();    if (X1 < x2) {        return-1;//x < y    }    if (X1 > x2) {        return 1;//x > Y    }    return 0;// x = = y}); [' Apple ', ' Google ', ' Microsoft ']

  

Array manipulation of array APIs (filter, map, some, every, sort)

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.