Array.prototype.splice

Removes elements from an array and, if provided, inserts new elements in their place, returning the deleted elements.

Syntax

splice(start, deleteCount, ...items?)

Parameters

Return value

An array containing the elements that were deleted.

Examples

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