The importance of arrays in each programming language is self-evident, but in previous JavaScript arrays (JavaScript arrays in detail) have been very powerful, but the methods of operation are not perfect, and they have been properly supplemented in ECMAScript5.
Array.isarray (Element)
This is a static function of the array object that is used to determine whether an object is not an array
var a = new Array (123); var B = new Date (); Console.log (Array.isarray (a)); True Console.log (Array.isarray (b));//false
. IndexOf (Element)/. LastIndexOf (Element)
As the name implies, these two methods are used to find the position of the specified element within the array, find the first one to return its index, and not find the return -1,indexof to search from beginning to finish, LastIndexOf reverse search.
var a=new Array (1,2,3,3,2,1); Console.log (A.indexof (2)); 1 console.log (A.lastindexof (2));//4
. ForEach (Element,index,array)
Iterate through the array, the parameter is a callback function, the callback function has three parameters: the current element, the element index, the entire array
var a=new Array (1,2,3,4,5,6); A.foreach (function (e,i,array) { array[i]=e+1; }); Console.log (a); [2, 3, 4, 5, 6, 7]
. Every (function (Element,index,array))/. Some (function (Element,index,array))
These two functions are similar to the logical decision in discrete mathematics, and the callback function returns a Boolean value that returns True when each callback function of the "All" function returns true and terminates execution when the every is encountered, returning the False;some function as "exists" A callback function returns True when execution terminates and returns true, otherwise false is returned. Calling every on an empty array returns True,some returns FALSE.
var a=new Array (1,2,3,4,5,6); /*0:1 1:2 2:3 3:4 4:5 false */ Console.log (a.every (function (E,i,arr) { Console.lo G (i+ ': ' +e); return e<5; });
var a=new Array (1,2,3,4,5,6); /*0:1 1:2 2:3 3:4 4:5 true */ Console.log (A.some (function (E,i,arr) { console.log (i+ ') : ' +e); return e>4; });
. map (function (Element))
Similar to foreach, iterates through an array, returns a new array of callback functions, returns a new array with the same structure as the original array, and the original array unchanged
var a=new Array (1,2,3,4,5,6); Console.log (A.map (function (e) { return e*e; }));//[1, 4, 9, 3, 2] console.log (a);//[1,, 4, 5,, 6]
. filter (function (Element))
Returns a subset of the array in which the callback function is used to determine whether the logical return is returned, true to add the current element to the returned array, false to not add, the new array contains only values that return True, the index is missing, the original array remains unchanged
var a=new Array (1,2,3,4,5,6); Console.log (A.filter (function (e) { return e%2==0; }));//[2, 4, 6] Console.log (a);//[1, 2, 3, 4, 5, 6]
. reduce (function (V1,V2), value)/. Reduceright (function (V1,V2), value)
Iterate through the array, call the callback function, combine the elements of the array into a single value, reduce the index from the minimum value, reduceright the reverse, the method has two parameters
1. Callback function: The two values are combined into one, return the result
2.value, an initial value, optional
var a=new Array (1,2,3,4,5,6); Console.log (A.reduce (function (v1,v2) { return v1+v2; }));// Console.log (a.reduceright (function (v1, V2) { return v1-v2; },100)); 79
JS in Map,foreach,filter,reduce and other methods are ECMAScript5 array new method