Array in JavaScript can contain any data type and can access each element through an index.
Like Java, to get the length of Array, you can access the length attribute directly.
var arr=[1,2,3,'A','B','C'];
arr.length;//6
If you assign a new value to length directly, the size of Array will change.
var arr=[1,2,3];
arr.length; //The length is 3.
arr.length=6;
arr;//arr becomes [1,2,3,undefined,undefined,undefined]
arr.length=2;
arr;//arr becomes [1,2]
Array can modify the corresponding element to a new value by indexing
var arr=['A','B','C'];
arr[1]=99;
arr;//arrNow change['A','99','C']
If the index exceeds the range, it will also cause changes in Array size.
var arr=['A','B','C'];
arr[5]='X';
arr;//arr Turn into['A','B','C',undefined,undefined,'x']
Cross-border access to Array in JavaScript is error-free, but when writing code, it is not recommended to modify the size of Array directly. When accessing an index, make sure that the index does not cross-border.
inidexOf
Similar to String, Array can also search for the location of a specified element by indexOf().
var arr=[10,20,'30','a'];
arr.indexOf('30');//Index 2
arr.indexOf(30);//No 30 found, return - 1
slice
Slce () corresponds to substring() of String, which intercepts some elements of Array and returns a new Array
var arr=['A','B','C','D','E','F','G'];
arr.slice(0,3);//from0Start, go to3End, but not include3,['A','B','C']
arr.slice9(3);//from3From beginning to end,['D','E','F','G']
Slce () start and stop parameters include start index, not end index
If you don't pass any parameters to slice(), it truncates all elements from beginning to end. You can directly copy a new array
push and pop
push() adds several elements to the end of Array; pop() deletes the last element of Array
var arr=[1,2];
arr.push('A','B');
arr;//arrTurn into[1,2,'A','B']
arr.pop();//Return'B'
arr;//arrTurn into[1,2,'A']
arr.pop();arr.pop();arr.pop();//Consecutive deletion3Second, at this time arr There are no elements.
arr;//arr=[]
arr.pop();//Empty array pop No mistake. Death returns. undefined
arr;//arr=[]
unshift and shift
unshift() adds several elements to Array's head, shift() deletes Array's first element
var arr=[1,2];
arr.unshift('A','B');
arr;//arr=['A','B',1,2]
arr.shift();//Return'A'
arr//arr=['B',1,2]
arr.shift();arr.shift();arr.shift();//Consecutive deletion3Second, at this time arr=[]
arr;//arr=[]
arr.shift();//Empty arrayshiftNo error, but return undefined
sort
sort() can sort Array, directly modify Array's element location, and when called directly, sort Array in the default order
var arr=['B','C','A'];
arr.sort();
arr;//arr=['A','B','C']
reverse
reverse() can invert Array's elements as a whole
var arr=['three','two','one'];
arr.reverse();
arr;//arr=['one','two','three']
splice
splice() can delete elements from a specified location, and then add elements from that location
var arr=[1,2,3,'A','B','C'];
arr.splice(2,3,'D','E');//From the first2Indices begin to be deleted3Five elements, return['A','B','C']
arr;//arr=[1,2,'D','E','C']
//Delete only and not add
arr.splice(2,2);//Return['A','B']
arr;//arr=[1,2,'C']
//Add but not delete
arr.splice(2,0,'F','G');//Return[]
arr;//arr=[1,2,'F','G','C']
concat
The concat() method connects the current Array to another Array and returns a new Array.
var arr=['A','B','C'];
var arr2=[1,2,3];
var arr3=arr.concat(arr2);
arr3;//arr3=['A','B','C',1,2,3]
arr;//arr=['A','B','C']
Note that the concat() method does not modify the current Array, but instead returns a new Array.
The concat() method accepts any element and Array and automatically disassembles Array and adds it all to the new Array.
var arr=['A','B','C'];
arr.concat(1, 2, [3, 4]); // ['A', 'B', 'C', 1, 2, 3, 4]
join
The join() method connects each element of the current Array with a specified string and returns the connected string.
var arr=['A','B','C'];
arr.join("~");//Return to'A~B~C'