ES6 Array Correlation

Source: Internet
Author: User

A few new ways to ES6 arrays:

1. ForEach ()

// ForEach () iterates through an array with no return value and does not change the original array var arr=[1,2,3,4]arr.foreach ((item,index,arr)={    console.log (item);     // 1,2,3,4})

2. Map ()

// map () iterates through an array, returns a new array, and does not change the value of the original array var arr=[1,2,3,4]arr.map ((item,index,arr)={    return item*10;    // returns the new array 10,20,30,40})

3. Filter ()

// filters the values in the array that do not satisfy the condition, returns a new array, does not change the value of the original array var arr=[1,2,3,4]arr.filter ((item,index,arr)={    return Item >2;   // the new array is [3,4]})

4. Reduce ()

// reduce lets the front and back of the array perform some sort of calculation, then returns its value and continues the calculation. Does not change the original array, returns the final result of the calculation, and begins the traversal from the second item of the array.  var arr=[1,2,3,4]arr.reduce ((result,item,index,arr)={     Console.log (result);   // 1  3  6  result for the last computed     console.log (item);    // 2  3  4     console.log (index);   // 1  2  3     return Result+item;   // ( ten})

5.some ()

// iterate through each item of the array, with one item returning true, stopping the traversal, and the result returns TRUE. Do not change the original array var arr=[]arr.some ((item,index,arr)={    return item>3;  // The result is true})

6. Every ()

// iterates through each item of the array, each item returns TRUE, and the final result is true. When any one of the items returns false, stop the traversal and return false. Do not change the original array var arr =[1,2,3,4 = = = {    return//  The result is False})

ES6 Array de-weight

1. Method One

function Unique (arr) {// defines a constant res with a value of a Map object instance Const res=new  map (); // returns the result of an array of arr filters, resulting in an array // The filter condition is that if a key is not in the res, the key to set this value is 1 return Arr.filter ((a) =>!res.has (a) && Res.set (a,1))}

2. Method Two

// The Array.from () method creates a new array instance from an array-like or iterative object, including the Array,map,set,string,typedarray,arguments object, and so on . function Unique () {    return array.from (new  Set (arr));}

ES6 Array Correlation

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.