Use the splice method of the array to add, delete, and replace the array (distinguish the slice method)

Use the splice method of an array to add, delete, and replace an array

First of all, to use the splice method, we must first distinguish the slice method. Both are array methods. Although the method names are similar, the functions are different.

slice() interception

slice() interception

This method can create a new array based on one or more items in the current array

It takes one or two parameters, the starting position and the ending position (not including the ending position) to intercept. If there is only one parameter, all items from the starting position to the end are intercepted.

If there is a negative number in the parameter, then use the length of the array plus the result of the parameter as the actual parameter. For example, when the length of the array is 5, slice(-2,-1) has the same effect as slice(3,4). If the end position is smaller than the start position, an empty array will be returned
let arr = [1,2,3,4,5,6,7]
console.log(arr.slice(1,4));//[2,3,4]
console.log(arr);//[1,2,3,4,5,6,7] 该方法不改变原数组

console.log(arr.slice(1));//[2,3,4,5,6,7]

console.log(arr.slice(-2,-1));//相当于arr.slice(5,6)  输出[6]
console.log(arr.slice(5,6));//输出[6]

//结束位置小于开始位置,返回空数组
console.log(arr.slice(-1,-4));//输出[]
console.log(arr.slice(6,3));//输出[]

splice()

The most powerful method of splice() array, it can delete, add, replace

Realize deletion : specify two parameters, parameter 1: starting position parameter 2: number of items to be deleted, such as splice(0,2) to delete the first two items

**Realize addition: **Specify three parameters, parameter 1: starting position parameter 2: 0 (represents the number of items to be deleted, 0 means only insert but not delete) parameter 3: item to be added (if there are multiple items to be added, you can continue to pass in multiple items) such as splice(1,0,"22", "99") to add items from subscript 1 of the array

** Realize replacement: ** Add any number of items from the specified position, and delete any number of items at the same time, you need to specify three parameters, parameter 1: the starting position parameter 2: the number of items to be deleted parameter 3: any number of items to be inserted. The number of inserted items does not have to be equal to the number of deleted items. For example, splice(2,1,"k",'l') deletes the item at position 2 of the array, and then inserts a new element from position 2

The splice() method always returns a new array containing the items that were removed, or an empty array if none were removed

achieve delete

 //实现删除
 let arr = [1,2,3,4,5,6,7]
 console.log(arr.splice(0,2));//[1,2] 返回被删除的元素
 console.log(arr);//[3,4,5,6,7] 该方法会改变原数组

implement add

let arr = [1,2,3,4,5,6,7]
console.log(arr.splice(1,0,100,200));//[]返回空数组,没有被删除的元素
console.log(arr);//[ 1, 100, 200, 2, 3, 4, 5, 6, 7 ]  改变原数组

achieve replacement

 let arr = [1,2,3,4,5,6,7]
 console.log(arr.splice(2,1,"hello","world"));//[3]  返回被删除的元素
 console.log(arr);//[1,2,"hello","world",4,5,6,7]
let arr = [1,2,3,4,5,6,7]
console.log(arr.splice(1,6,"hello"));//[2,3,4,5,6,7]  返回被删除的元素
console.log(arr);//[1,"hello"]

Guess you like

Origin blog.csdn.net/m0_48895748/article/details/127612854