Modifier prevention: changes the value of the object calling them
1. copyWithin(), inside the array, copy a sequence of elements to another sequence of elements, and overwrite the original value
Usage -- copyWithin(target,start,end)
Parameters: target -- copy where to start insertion start -- copy where to start the element end -- copy where to end the element
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
arr1.copyWithin(3, 4, 5);
console.log(arr1); //[ 1, 2, 3, 5, 5, 6, 7, 8 ]
console.log(arr1); //[ 1, 2, 3, 5, 5, 6, 7, 8 ]
2. fill(), fill all elements in an array from the start index to the end index with a fixed value. Exclude termination index
Usage: array.fill(value,star,end)
Parameter: Value -- replacement value start -- start position, default is 0 end -- end position, default is array.length
var arr1 = [4, 6, 9, 2, 43, 5, 1];
var arr2 = [4, 6, 9, 2, 43, 5, 1];
arr1.fill('a', 0, 3);
arr2.fill('b');
console.log(arr1); //[ 'a', 'a', 'a', 2, 43, 5, 1 ]
console.log(arr2); //[ 'b', 'b', 'b', 'b', 'b', 'b', 'b' ]
3. pop(), delete the last element of the array
var arr = [5, 2, 7, 9, 3];
console.log(arr.pop()); //3
console.log(arr); //[ 5, 2, 7, 9 ]
4.push() adds one or more elements to the array by default, and returns the length of the new array
var arr = [3, 6, 9, 2, 4];
console.log(arr.push('h', 'i')); //7
console.log(arr); //[ 3, 6, 9, 2, 4, 'h', 'i' ]
5. reverse(), reverse the order of elements
var arr = [6, 3, 8, 9];
console.log(arr.reverse()); //[ 9, 8, 3, 6 ]
console.log(arr); //[ 9, 8, 3, 6 ]
6. shift(), delete the first element of the array
var arr = [6, 3, 2, 6, 8, 5];
console.log(arr.shift()); //6
console.log(arr); //[ 3, 2, 6, 8, 5 ]
7. sort() to sort the array
var arr1 = ['c','a','d','b'];
var arr2 = [4,7,1,9,12,0];
console.log(arr1.sort()); //[ 'a', 'b', 'c', 'd' ]
console.log(arr1); //[ 'a', 'b', 'c', 'd' ]
console.log(arr2.sort(function(a,b){return a-b})); //[ 0, 1, 4, 7, 9, 12 ]
console.log(arr2); //[ 0, 1, 4, 7, 9, 12 ]
8. splice(), add or delete any element to the array at any position
Usage: splice(index,howmany,item,...item)
Parameter: index - required, where the element starts to modify howmany - required, number of elements deleted item...item - optional, new element
var arr1 = [3,7,9,14];
var arr2 = [3,7,9,14];
var arr3 = [3,7,9,14];
console.log(arr1.splice(1,0,'a','b')); //[]
console.log(arr1); //[ 3, 'a', 'b', 7, 9, 14 ]
console.log(arr2.splice(0,2,'hi')); //[ 3, 7 ]
console.log(arr2); //[ 'hi', 9, 14 ]
console.log(arr3.splice(0,1)); //[3]
console.log(arr3); //[ 7, 9, 14 ]
9. unshift(), add one or more elements at the beginning of the array, and return the array length
var arr = [4,7,2,6,8,2];
console.log(arr.unshift(0,1,4)); //9
console.log(arr); //[ 0, 1, 4, 4, 7, 2, 6, 8, 2 ]