Lists in Python can insert data in the middle:
>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> a.insert(3,10)
>>> a
[1, 2, 3, 10, 4, 5, 6, 7]
But there seems to be no insert function in nodejs, and deleting the middle elements is not complete.
> a= [1, 2, 3, 4, 5, 6]
[ 1,
2,
3,
4,
5,
6 ]
> delete a[2]
true
> a
[ 1,
2,
,
4,
5,
6 ]
If you want to delete a[2] and get a new list of [1,2,4,5,6,7], what should you do?
If you want to insert data 10 after the third position and get [1,2,3,10,4,5,6], what should you do?
Correct answer upstairs
Attached is the usage of splice
Thesplice() method adds/removes items to/from the array and returns the removed item.
Note: This method will change the original array.
Grammar
index required. An integer specifying the position at which to add/remove an item. Use a negative number to specify the position from the end of the array.
howmany required. The number of items to delete. If set to 0, items will not be deleted.
item1, ..., itemX optional. New items added to the array.
Return Value
Array A new array containing the deleted items, if any.
Description
The splice() method removes zero or more elements starting at index and replaces those removed elements with one or more values declared in the parameter list.
If an element is deleted from arrayObject, the array containing the deleted element is returned.
The original poster didn’t know the power of splice.
splice(start,deleteCount,val1,val2,...):
Delete the deleteCount item from the start position, and insert val1, val2,... from this position. You can extend the prototype method of Array yourself:This way you will become familiar