[ Declare an array] var a=[1,2,3];
[ Define the length of the array] var a=new Array(2);
[ Connect array] array 1.concat(array 2, array 3)
[ Convert the array to a string, you can customize the connector] arr.join("connector")
[ Convert an array to a string and return an array] arr.toString()
[ Return the last digit of the array and delete the value] arr.pop() [Display the last digit first, then delete it]
[ Return the last digit of the array and delete the value] arr.shift() [Display the first digit first, then delete it]
[ Add one digit to the end of the array and return the length number] arr.push (the first added, the second added)
[ Add one digit to the first position of the array and return the length number] arr.unshift (the first added, the second added)
[ Reverse the array] arr.reverse()
[ Intercept array (including start and end positions)] arr.slice (start position, end position)
[ Sort by the first letter of the text] arr.sort();
[ Sort by number size] arr.sort(sortNumber); [The following function needs to be added above]
var arr = []; arr[0]=a; arr[1]=b;
console.log(arr); // a arr.push('d','e','f'); console.log(arr); // (4) ["b", "d", "e", "f" ]
2. The unshift() method adds one or more elements to the beginning of the array
arr.unshift(1,2); //同 1 例子
console.log(arr); //[1,2,"b","d","e","f"]
res=arr.shift(); //删除的第一个元素是1,返回1 console.log(res); // 1
三、pop()方法用于删除数组最后一个元素,并返回最后一个元素
var res=arr.pop(); //同 1 例子 console.log(res); // f console.log(arr) //(4) [ "a", "b", "d","e"]
var arr=new Array(3) arr[0]="a" arr[1]="b" arr[2]="c" console.log(arr) //(3) ["a", "b", "c"] console.log(arr.push("d")) // 4 console.log(arr) //(4) ["a", "b", "c", "d"]
var a=[1,2,3]; console.log(a.concat(4,5)) //[1,2,3,4,5]
var arr2=new Array(3) arr2[0]='abc' arr2[1]='john' arr2[2]='Toni' console.log(arr2.join()) //abc,john,Toni
var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" console.log(arr) //["george","John","Thomas","James"] console.log(arr.sort()) //
var arr2=[1,3,2,6,4]
console.log(arr2.sort()) //[1,2,3,4,6]
八、reverse():将数组反序 , 方法用于颠倒数组中元素的顺序。
var arr4=new Array(3) arr4[0]='abc' arr4[1]='john' arr4[2]='Toni' console.log(arr4.reverse()) //['Toni','John','abc']
var a=[1,2,3,4,5] var b=a.slice(2,4); console.log(b); // 3,4
var a = [1,2,3,4,5]; var b = a.splice(2,2,7,8,9); console.log(a) //[1,2,7,8,9,5] console.log(b) //[3,4]
var arr6=new Array(3) arr6[0] = "George" arr6[1] = "John" arr6[2] = "Thomas" console.log(arr6) // ["George", "John", "Thomas"] arr6.splice(1,1,"123") console.log(arr6) //["George", "123", "Thomas"]