JAVASCRIPT_ECMA5 Array new features

Source: Internet
Author: User
Tags prev

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


New Location method: IndexOf lastIndexOf
1.1 parameters indicating the value of the return index position (index starting from 0)
var index = Arr.indexof (4);
alert (index); 3


2.2 parameters when the first parameter indicates the starting position the second argument or the value
var index = arr.indexof (bis);
alert (index);//5


3. They look for array comparisons when ' = = = '
LastIndexOf
var index = Arr.lastindexof (2);
alert (index);//7

5 Methods for new iterations

1.every: Run a function for each element of the array if all returns True finally returns True if there is a return false the last result then 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 to run the given function to perform the filtered result back
var result = Arr.filter (function (item, index, array) {
return item > 2;
});
alert (result); 3,4,5,4,3

3.forEach: Loop The value of each item in the array and execute a method
Arr.foreach (function (item, index, array) {
alert (item);//1,2,3,4,5,4,3,2,1
});

4.map running a function for each element of an array can return a new result after the function has finished executing
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 the array if there is an item that returns true finally returns True if each item returns false The last result returns false
var result = Arr.some (function (item, index, array) {
return item >5;
});
alert (result);//false

Reduce reduceright
Variable start position is different


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;

JAVASCRIPT_ECMA5 Array new features

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.