The use of the splice method in JavaScript

Article directory

Overview:

The splice in JavaScript is mainly used to operate on arrays in js, including deletion, addition, replacement, etc.

function method

  1. 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 image description here

  1. 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

insert image description here
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.
insert image description here

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)

Guess you like

Origin blog.csdn.net/u014212540/article/details/129416865