var arr = [ 1, 2, 3, 4, 5, 4, 3, 2, 1 ];
Method to add new position: indexOf lastIndexOf
1.1 parameters indicate the value to be passed back to the index position (index starts from 0)
var index = arr.indexOf(4);
alert(index); //3
2. When there are 2 parameters, the first parameter indicates the starting position. The second parameter or the value.
var index = arr.indexOf(4,4);
alert(index); //5
3. When they look for array comparisons '==='
lastIndexOf
var index = arr.lastIndexOf(2);
alert(index); //7
5 new iteration methods
1.every: Run a function for each element of the array. If true is returned, it will return true. If there is a return false, it will return false.
var result = arr.every(function(item , index , array){ return item >= 1 ; });alert(result); //true2.filter: Run a function for each element of the array. The given function is executed to return the filtered result.
var result = arr.filter(function(item , index , array){return item > 2 ;});alert(result); //3,4,5,4,33.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 Run a function for each element of the array and can return the new result after the function is executed.
var result = arr.map(function(item, index, array){ return item*10;});alert(result); //10,20,30,40,50,40,30,20,105.some: Run a function for each element of the array. If there is an item that returns true, it will return true. If each item returns false, it will return false.
var result = arr.some(function(item, index, array){ return item >5 ;});alert(result); //falsereduce reduceRight
The starting position of the variable 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;The above detailed explanation of the new features of JavaScript_ECMA5 array is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.