splice
Insert and remove elements from an observe array.
list.splice(index[, howMany[, ...newItems]])
splice inserts and removes elements from an observe array.
import { observe } from "can/everything";
const names = new observe.Array(['Alice', 'Bob', 'Chris']);
console.log(names.splice(0, 1)); //-> ['Alice']
console.log(names); //-> ['Bob', 'Chris']
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 from theindexto the end of the array. - newItems
{*}:Items to insert into the array
Returns
{Array}:
The elements removed by the splice.
Use
splice lets you remove and insert items into an observe array.
This example shows replacing an item at a given index:
import { observe } from "can/everything";
const names = new observe.Array(['Alice', 'Bob', 'Chris']);
console.log(names.splice(1, 1, 'Dave')); //-> ['Bob']
console.log(names); //-> ['Alice', 'Bob', 'Chris']
Events
splice causes length events to be fired.