Array.splice
splice — Cuts a piece out of an array, and optionally put in new value(s) in its place.
Description
Array.splice(integer start, integer length, [mixed item, …])
Parameters
Name |
Description |
Type |
Default |
Optional |
start |
|
integer |
|
No |
length |
|
integer |
|
No |
item, … |
|
mixed |
|
Yes |
Return values
The cut out piece array.
Examples
Example #1 – splice example
var array = [2, 4, 8, 16, 32];
console.log(array); // [2, 4, 8, 16, 32]
console.log(array.splice(1, 2)); // [4, 8]
console.log(array); // [2, 16, 32]
Example #2 – splice example
var array = [2, 4, 8, 16, 32];
console.log(array); // [2, 4, 8, 16, 32]
console.log(array.splice(1, 2, 5, 7, 9)); // [4, 8]
console.log(array); // [2, 5, 7, 9, 16, 32]
Example #3 – splice example
var array = [2, 4, 8];
console.log(array); // [2, 4, 8]
console.log(array.splice(1, 4, 11)); // [4, 8]
console.log(array); // [2, 11]
Example #4 – splice example
var array = [2, 4, 8, 16, 32];
console.log(array); // [2, 4, 8, 16, 32]
console.log(array.splice(1, array.length - 2, 11)); // [4, 8, 16]
console.log(array); // [2, 11, 32]
External references