1. Splice method in js
splice(index,len,[item]) Note: This method changes the original array.
splice has 3 parameters, which can also be used to replace/delete/add one or several values in the array.
index: Array start index len: Replace/delete length item: Replace value, item is empty if delete operation
For example: arr = ['a','b','c','d']
Delete --- item not set
arr.splice(1,1) //['a','c','d'] delete a value whose starting subscript is 1 and length is 1. len sets 1. If it is 0, the array remains unchanged.
arr.splice(1,2) //['a','d'] Delete a value with the starting subscript of 1 and length 2, len set 2
Replace--- item is the value of the replacement
arr.splice(1,1,'ttt') //['a','ttt','c','d'] Replace the starting subscript to 1, a value with length 1 is 'ttt', len set 1
arr.splice(1,2,'ttt') //['a','ttt','d'] Replace the starting subscript to 1, the two values with length 2 are 'ttt', and the len set 1
Add ---- len is set to 0, item is the added value
arr.splice(1,0,'ttt') //['a','ttt','b','c','d'] means adding an 'ttt' at the subscript of 1
It seems that splice is the most convenient
2. After delete delete delete delete delete delete, the value marked under the array will be undefined, and the length of the array will not change.
For example: delete arr[1] //['a', ,'c','d'] appears in the middle of two commas, the length of the array remains unchanged, and one is undefined
There are several other customization methods, please refer to here
The above article Js's method of deleting a certain item or item in the array is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.