Array addition and deletion
1. Add and delete tail
push() is added at the end, and the parameter is the item to be added; If you add more than one, separate them with commas; The original array will change without assignment
pop() is deleted at the end. No parameters need to be written. The deleted items are returned
2. Header addition and deletion
unshift() header. The parameter is the item to be added; If you add more than one, separate them with commas; The original array will change without assignment
The shift() header is deleted without writing parameters, and the deleted items are returned
3.splice( a,b,c,d...) Method
a: subscript
b: number (if it is 0, it is inserted; if there is a number, it is deleted)
c,d... Replaced value
In the replaceable array, the specified value (starting from the subscript) returns the replaced value
You can insert a new item at the specified location (the insertion is before the subscript)
Can be deleted (including subscripts)
Example: in the replaceable array, the specified value (starting from the subscript) returns the replaced value
var arr = ['a','b','c','d','e']; // 0 1 2 3 4
var arr1 = arr.splice(3,2,'x','y','z') console.log(arr); //['a', 'b', 'c', 'x', 'y', 'z'] console.log(arr1); //Returned is:Replaced value['d', 'e']
Example: you can insert a new item at the specified position (the insertion is before the subscript). The return is []
var arr = ['a','b','c','d','e']; // 0 1 2 3 4
var arr1 = arr.splice(1,0,'x','y','z') console.log(arr); //['a', 'x', 'y', 'z', 'b', 'c', 'd', 'e'] console.log(arr1); //Returned is:Replaced value[ ]
Example: you can delete (including subscripts) and return deleted items
var arr = ['a','b','c','d','e']; // 0 1 2 3 4
var arr1 = arr.splice(1,1) console.log(arr); //['a', 'c', 'd', 'e'] console.log(arr1); //Returned is:Replaced value[ 'b' ]
4. Slice (a, b) method {is used to get the substring (the original array remains unchanged, and the returned array is a new array), which is similar to the slice() of the string
A subscript (including a)
b subscript (excluding b)
If the second parameter (b) is not provided, it will be intercepted to the end
5. join() method of array and split() method of string
The original array of join() does not change
Leave blank. The default value is comma separated, just like the toString() method
Example:
var arr = [1,2,3,4,5,6]; var arr1 = arr.join(); var arr2 = arr.join('&'); console.log(arr); // [1, 2, 3, 4, 5, 6] console.log(arr1); // 1,2,3,4,5,6 console.log(arr2); // 1&2&3&4&5&6
split() the original array does not change
Cannot leave blank
Example:
var str = '123456' var str1 = str.split() console.log(str1); // ['123456'] var str2 = str.split('') console.log(str2); // ['1', '2', '3', '4', '5', '6'] var str3 = str.split(' ') //There is a space console.log(str3); // ['123456'] console.log(str); // '123456'
6.concat() method} can merge multiple arrays without changing the original array
var arr1 = [1,2,3]; var arr2 = [4,5]; var arr3 = [6,7,8]; var arr = arr1.concat(arr2); console.log(arr); // [1,2,3,4,5] console.log(arr1); // The original array does not change : [1,2,3] var arr4 = arr1.concat(arr2,arr3); console.log(arr4); // [1, 2, 3, 4, 5, 6, 7, 8]
7. The reverse () method {reverses the array and directly changes the original array
var arr = [1,2,3,4,5] var arr1 = arr.reverse(); console.log(arr); // Will change the original array: [5, 4, 3, 2, 1] console.log(arr1); // [5, 4, 3, 2, 1]
8. Difference between indexof() and includes()
indexOf() cannot be found, return - 1; If indexOf has 2 parameters, it is - 1 and can only have one parameter
includes() cannot be found and returns false