JAVASCRIPT_ECMA5 array new features detailed knowledge _ Basics

Source: Internet
Author: User
Tags prev

var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];

New Add Position method: IndexOf LastIndexOf

When 1.1 arguments are passed, the value returns the index position (index starting from 0)

var index = Arr.indexof (4);
alert (index); 3

2. When the first parameter represents the second argument or value at the start position of the 2 parameter

var index = arr.indexof (4,4);
alert (index); 5

3. They look for array comparisons when ' = = '

LastIndexOf
var index = Arr.lastindexof (2);
alert (index); 7

5 New iterative methods

1.every: Run a function for each element of an array if both returns True last returns True if one returns false the last result returns false

var result = Arr.every (function (item, index, array) {return
item >= 1;
});
alert (result); True

2.filter: For each element of the array, perform a function of the given function to execute the filtered result returned

 var result = Arr.filter (function (item, index, array) {return
item > 2;
});
alert (result); 3,4,5,4,3

3.forEach: Loops The value of each item in the array and executes a method

 Arr.foreach (function (item, index, array) {
alert (item);//1,2,3,4,5,4,3,2,1
}); 

4.map the operation of a function for each element of an array can be performed after the function completes and the new result is returned

var result = Arr.map (function (item, index, array) {return
item*10;
});
alert (result); 10,20,30,40,50,40,30,20,10   

5.some: Run a function for each element of an array if one of the entries returns True, the last returns true if each entry returns false the final result returns false

var result = Arr.some (function (item, index, array) {return
item >5;
});
alert (result); False 

Reduce reduceright

Variable starts at different positions

Previous value, current value, index position, array

var result = Arr.reduce (function (prev, cur, index, array) {return
prev + cur;
});
Alert (result)//25;

var result = Arr.reduceright (function (prev, cur, index, array) {return
prev + cur;
});
Alert (result)//25;

The above JAVASCRIPT_ECMA5 array of new features is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.