Array element deletion, replacement and addition

Syntax: splice(index,len,[item]) This method will change the original array.
splice has 3 parameters, it can also be used to replace, delete, add, or add one or several values ​​in the
array. index indicates the start subscript of the array.
len
. delete: var arr = ['a','b','c','d'];
arr.splice(1,2);
console.log(arr);----> the output is ['a' ,'d']
2 replace: var arr2 = ['a','b','c','d'];
arr2.splice(1,2,'ttt');
console.log(arr2);- ---> The output is ['a','ttt','d']
3. Add (make len be 0, item is the added value):
var arr = ['a','b','c' ,'d'];
arr.splice(1,0,'ttt');
console.log(arr);-----The output is ['a','ttt','b','c', 'd'] 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327072608&siteId=291194637