Article directory
Overview:
The splice in JavaScript is mainly used to operate on arrays in js, including deletion, addition, replacement, etc.
function method
- Delete function, the first parameter is the position of the first item, the second parameter is the number of
array.splice(index,num) to be deleted, the return value is the deleted content, and array is the result value.
- Insert function, the first parameter (insert position), the second parameter (0), the third parameter (inserted item)
array.splice(index,0,insertValue), the return value is an empty array, and the array value is final result value
3. Replacement function, the first parameter (starting position), the second parameter (number of deleted items), the third parameter (insert any number of items) array.splice(
index,num,insertValue), return value For deleting content, array is the result value.
Summarize
1. Delete - used to delete elements, two parameters, the first parameter (the position of the first item to be deleted), the second parameter (the number of items to be deleted) 2. Insert - insert any item element
to the specified position of the array . Three parameters, the first parameter (insert position), the second parameter (0), the third parameter (inserted item)
3. Replacement - insert any item element to the specified position of the array, and delete any number of items at the same time, Three parameters. First parameter (starting position), second parameter (number of items to remove), third parameter (any number of items to insert)