Array.splice(start, [deleteCount], [item1...N]) Last updated: 22. Jul 2024

Changes the contents of an array by removing or replacing existing elements and/or adding new elements at specified index.

Parameters

Name Type Description
start int

Index where to add and remove

deleteCount (optional) int

Count of items to remove

item1...N (optional) object

Items to add (specify from zero to many params)

Returns

An array of the items that were removed.

Example

Code example (JS)

JS is normal JavaScript either running in the browser or on the Docly™ server.
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
/* Inserts at index 1 */
write(months);
/* Expected output: Array ["Jan", "Feb", "March", "April", "June"] */

months.splice(4, 1, 'May');
/* Replaces 1 element at index 4 */
write(months);
/* Expected output: Array ["Jan", "Feb", "March", "April", "May"] */

Output

The JS code above produces the output shown below:
["Jan","Feb","March","April","June"]["Jan","Feb","March","April","May"]