New Features of JavaScript_ECMA5 array, ecmajavascript
Var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Method for adding a new location: indexOf lastIndexOf
When the first parameter is set, it indicates that the index position is returned by passing the value (index starts from 0)
Var index = arr. indexOf (4 );
Alert (index); // 3
2. When there are two parameters, the first parameter indicates the start position.
Var index = arr. indexOf (4, 4 );
Alert (index); // 5
3. When they look for Array comparison, '='
LastIndexOf
Var index = arr. lastIndexOf (2 );
Alert (index); // 7
Five new iteration methods
1. every: run a function on each element of the array. If true is returned, true is returned. If one element returns false, false is returned.
var result = arr.every(function(item , index , array){ return item >= 1 ; });alert(result); //true
2. filter: execute a function for each element of the array to run the given function and return the filtered result.
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 can run a function on each element of the array. After the function is executed, 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 on each element of the array. If one item returns true, true is returned. If each element returns false, false is returned.
var result = arr.some(function(item, index, array){ return item >5 ;});alert(result); //false
Reduce reduceRight
The starting position of the variable is different.
Previous value, current value, index location, 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 new features of the above JavaScript_ECMA5 array are all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's home.