The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, you should use the slice() method.
Syntax:
splice(start) splice(start, deleteCount) splice(start, deleteCount, item1) splice(start, deleteCount, item1, item2, itemN)
Parameters
start
- The index at which to start changing the array.
If greater than the length of the array,
start
will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many element as item[n*] provided.If negative, it will begin that many elements from the end of the array. (In this case, the origin
-1
, meaning-n
is the index of then
th last element, and is therefore equivalent to the index ofarray.length - n
.) Ifstart
isnegative infinity
, it will begin from index0
. deleteCount
Optional- An integer indicating the number of elements in the array to remove from
start
.If
deleteCount
is omitted, or if its value is equal to or larger thanarray.length - start
(that is, if it is equal to or greater than the number of elements left in the array, starting atstart
), then all the elements fromstart
to the end of the array will be deleted.If
deleteCount
is0
or negative, no elements are removed. In this case, you should specify at least one new element. item1, item2, ...
Optional- The elements to add to the array, beginning from
start
. If you do not specify any elements,splice()
will only remove elements from the array.
Return Value:
An array containing the deleted elements.
If only one element is removed, an array of one element is returned.
If no elements are removed, an empty array is returned.
Examples:
let myArray = ['A', 'B', 'C', 'D', 'E'] let removed = myArray.splice(1, 0, 'F') //1 means: Find 2nd value in array. //0 means: Don't delete anything. You will add the 3rd parameter which is 'F' to the place of 2nd value of array (the place of 'C') //'F' means: Add this // myArray is ['A', 'B', 'F', 'C', 'D', 'E'] // removed is [], no elements removed ------------------------------------------------------------- let myArray = ['A', 'B', 'C', 'D', 'E'] let removed = myArray.splice(4, 0, 'G', 'S') //1 means: Find 5th value in array. //0 means: Don't delete anything. You will add the 3rd and 4th parameters which are 'G' and 'S' to the place of 5th value of array (the place of 'E') //'G', 'S' means: Add these // myArray is ['A', 'B', 'C', 'D', 'G', 'S', 'E'] // return value, removed is [], no elements removed ------------------------------------------------------------- let myArray = ['A', 'B', 'C', 'D', 'E'] let removed = myArray.splice(3, 1) //3 means: Find 4th value in array. //1 means: Start to delete from 4th value. And delete only 1 value // myArray is ['A', 'B', 'C', 'E'] // return value, removed is ['D'] ------------------------------------------------------------- let myArray = ['A', 'B', 'C', 'D', 'E'] let removed = myArray.splice(1, 1, 'F') //2 means: Find 3rd value in array. //1 means: Start to delete from 3rd value. And delete only 1 value //'F' means: Delete 'B' and add this 'F' instead of it // myArray is ['A', 'F', 'C', 'D', 'E'] // return value, removed is ['B'] -------------------------------------------------------------