Declaring an array
var arr1 = [1,2,3,4,5]; var New Array (+); // declares a arr2 array of length 100. Arr2=[]; Arr2.length = +; var New //is a two-dimensional array. ARR3 = [[1,2,3,4,5]]; arr3.length = 1; arr3[0] = [1,2,3,4,5];arr3[0].length = 5; var New Array (for each); // ARR3 = [1,2];arr3.length = 2;
Before adding unshift () Remove shift () after adding push () to remove pop ()
varARR1 = [1,2,3,4,5]; Arr1.unshift (5,6);//arr1 = [5,6,1,2,3,4,5]; Unshift: Adds one or more elements at the beginning of the collection and returns a new array varARR2 = [1,2,3,4,5]; Arr2.shift (); //arr2 = [2,3,4,5] Shift: Removes the first element from the collection and returns the new array varARR3 = [1,2,3,4,5]; Arr3.push (5,6);//ARR3 = [1,2,3,4,5,6] Push: The trailing element in the collection and returns the new array returned varARR4 = [1,2,3,4,5]; Arr4.pop (); //ARR4 = [1,2,3,4] Pop: Removes the last element from the collection and returns a new array varARR5 = [1,2,3,4,5]; Arr5.length= 3;//ARR5 = [n/a]; the array is quickly deleted and a new array is returned.
The Intercept Arr.slice (start,end) method of the array start and end refers to the subscript, which does not contain an end element
var arr = [1,2,3,4,5,6,7,8,9 4) + ' <br/> '); // output: 5,6,7,8,9 Document.writeln (Arr.slice ( -4) + ' <br/> '); // output: 6,7,8,9 Document.writeln (Arr.slice (0,4) + ' <br/> '); // output: 1,2,3,4 Document.writeln (arr.slice + ' <br/> '); // output: 2 Document.writeln (Arr.slice (3,-2) + ' <br/> '); // output: 4,5,6,7 Document.writeln (Arr.slice (1,100) + ' <br/> '); // output: 2,3,4,5,6,7,8,9
The slice (Start[,end]) method of the Array object returns the part of the array from the subscript [start,end] (the element that does not contain the subscript end) If the end parameter is not specified, the slice () method does not change the original array, starting with start to the end of the array. Can be assigned to a new array.
Parameters:
(1) Start: The array subscript that starts the intercept, if start is a negative number, indicates that the calculation starts at the end of the array.
(2) End: Ends the truncated array subscript, and if End is a negative number, the calculation begins at the tail of the array.
JS Array Method Array array declaration element additions and deletions, etc.