can-define
Version:
Create observable objects with JS dot operator compatibility
46 lines (30 loc) • 1.42 kB
Markdown
can-define/list/list.prototype.splice splice
can-define/list/list.prototype
can-define/list/list.prototype
Insert and remove elements from a DefineList.
`list.splice(index[, howMany[, ...newItems]])`
Removes `howMany` items at `index` and adds `newItems` in their place.
{Number} index Where to start removing or inserting elements.
{Number} [howMany] The number of elements to remove
If _howMany_ is not provided, `splice` will remove all elements from `index` to the end of the DefineList.
{*} newItems Items to insert into the DefineList
{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:
```js
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.