37
loading...
This website collects cookies to deliver better user experience
slice(start, end);
start: the start index of the array where I should strat cut it.
end: the end index where I should stop cutting
const arr = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
arr.slice(1, 4); // will return ["Item 2", "Item 3", "Item 4"]
const arr = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
arr.slice(3); // will return ["Item 4", "Item 5", "Item 6", "Item 7", "Item 8"]
splice(start, deleteCount, item1, item2, itemN);
start: the start index of the array where I should start changing it.
deleteCount: the number of elements that I want to delete and if I don't want to remove anything we simply pass 0
Then the rest of the parameters is the elements we want to add to the array ^_^
const arr = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
arr.splice(2, 3);
console.log(arr); // will return ["Item 1", "Item 2", "Item 6", "Item 7", "Item 8"]
const arr = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
arr.splice(5, 0, "item 9", "item 10");
console.log(arr); // will return ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "item 9", "item 10", "Item 6", "Item 7", "Item 8"]
const arr = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"];
arr.splice(2, 3, "item 9", "item 10");
console.log(arr); // will return ["Item 1", "Item 2", "item 9", "item 10", "Item 6", "Item 7", "Item 8"]