var colors = new Array (20); Create an array with a length value of 20
var colors = new Array (' Red ', ' blue ', ' green '); Create an array with three strings;
You can also omit the new operation when using the array constructor, var colors = Array (3);
Array.isarray () method to determine if a value is an array
Some ways to do this:
Concat (); You can create a new array based on all the items in the current array
var colors = [' red ', ' green ', ' Blue '];
var colors2 = colors.concat (' Yellow ', [' black ', ' purple ']);
alert (colors)//Red,green,orange
Alert (COLORS2)//red,green,blue,yellow,black,purple
Slice (); Can accept one or two parameters, that is, the starting position and the end position, return all items starting at the end, but not the items that are ending, and in the case of only one argument, return all items from the specified position of the parameter to the end of the current array
Splice (); Change and delete
Three parameters, starting position, length, insert item;
Iterative methods:
Filter (); Use the specified function to determine whether an item is included in the returned array
var num= [1,2,3,4,5,4,3,2,1];
var result = Num.filter (function (Item,index,array) {
Return (item > 2);
})
alert (result);
Map ();
Also returns an array, similar to the one above
ForEach ();
var num= [1,2,3,4,5,4,3,2,1];
Num.foreach (function (Item,index,array) {
Perform certain actions
})
reduce (); and Reduceright (); Two methods for merging arrays
Loops through all the entries in the array and constructs a final return value that accepts 4 parameters: previous value, current value, index of item, and array object
var num= [1,2,3,4,5];
var result = Num.reduce (function (Prev,cur,index,array) {
return prev + cur;
})
alert (result); 15
JS Advanced Programming--array