JS inserts the and deletion method at the specified position of the array

  • 2021-07-10 18:29:09
  • OfStack

The splice () method adds/deletes items to/from the array and returns the deleted items.

Grammar

arrayObject.splice(index,howmany,item1,.....,itemX)

Parameter description

参数 描述
index 必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。
howmany 必需。要删除的项目数量。如果设置为 0,则不会删除项目。
item1, …, itemX 可选。向数组添加的新项目。

Instances

Add 1 element


var array = [1,2,3,4,6];
array.splice(4,0,5);

Results: array was [1, 2, 3, 4, 5, 6].

Delete and replace 1 element


var array = [1,2,2,4,5];
array.splice(2,1,2);

Results: array was [1, 2, 3, 4, 5].


Related articles: