The splice() method is used to remove and returns a slice of items starting at the specified index
Syntax:
collect(array).splice()
Parameters: The collect() method takes one argument that is converted into the collection and then splice() method is applied on it.
Return Value: This method returns a slice of items starting at the specified index.
Below example illustrate the splice() method in collect.js:
Example 1:
const collect = require('collect.js');
let arr = [8, 4, 7, 6, 5, 11, 3];
const collection = collect(arr);
const spliced = collection.splice(3);
let result1 = spliced.all();
let result2 = collection.all();
console.log(result1);
console.log(result2);
Output:
[6, 5, 11, 3] [8, 4, 7]
Example 2:
const collect = require('collect.js');
let arr = [8, 4, 7, 6, 5, 11, 3];
const collection = collect(arr);
const spliced = collection.splice(4, 5);
let result1 = spliced.all();
let result2 = collection.all();
console.log(result1);
console.log(result2);
Output:
[5, 11, 3] [8, 4, 7, 6]