splice
Insert and remove elements from a DefineList.
list.splice(index[, howMany[, ...newItems]])
Removes howMany items at index and adds newItems in their place.
Parameters
- index
{Number}:Where to start removing or inserting elements.
- howMany
{Number}:The number of elements to remove If howMany is not provided,
splicewill remove all elements fromindexto the end of the DefineList. - newItems
{*}:Items to insert into the DefineList
Returns
{Array}:
The elements removed by splice.
Use
splice lets you remove elements from and insert elements into a DefineList.
This example demonstrates how to do surgery on a list of numbers:
import {DefineList} from "can";
const list = new DefineList([0, 1, 2, 3]);
// starting at index 2, remove one element and insert 'Alice' and 'Bob':
list.splice(2, 1, 'Alice', 'Bob');
console.log(list.get()); //-> [0, 1, 'Alice', 'Bob', 3]
Events
splice causes the DefineList it's called on to emit
add events, remove events, and length events. If there are
any elements to remove, a remove event, and a
length event will be fired. If there are any elements to insert, a
separate add event, and a separate length event
will be fired.