Removes elements from an array and, if provided, inserts new elements in their place, returning the deleted elements.
splice(start, deleteCount, ...items?)
start
The index in the array from which to start removing elements.
deleteCount
The number of elements to remove (may be 0 if only insertion is needed ).
…items optional variadic
Elements to insert into the array in place of the deleted elements.
An array containing the elements that were deleted.
let a = ["tea", "coffee", "juice"]
a.splice(1, 1) // remove "coffee"
print(a)
tea,juice
let a = ["tea", "coffee", "juice"]
a.splice(2, 1, "milk") // replace "juice" with "milk"
print(a)
tea,coffee,milk
let a = ["tea", "coffee", "juice"]
a.splice(2, 0, "milk", "soda") // insert "milk" and "soda" after "coffee"
print(a)
tea,coffee,milk,soda,juice