English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
splice()The method changes the array by removing existing elements or adding new ones.
If you specify a different number of elements to insert than to delete, the length of the array will also be different.
Note: The splice() method will change the original array.
array.splice(start, deleteCount, [item1, item2, ...])
var months = [&39;Jan', &39;Mar', &39;Apr', &39;Jun']; months.splice(1, 0, 'Feb');// adding 'Feb'May 1Test and see‹/›
The numbers in the table specify the first browser version that fully supports the splice() method:
Method | |||||
splice() | 1 | 1 | is | is | 5.5 |
Parameter | Description |
---|---|
start | Start adding in the array/The index of the element to be removed. Negative values specify positions from the end of the array. |
deleteCount | (Optional) The number of elements to be removed. If set to 0, no elements will be removed |
item1, item2, ... | (Optional) Elements to be added to the array, starting fromstartingindexstartIf no elements are specified, splice() will only remove elements from the array. |
Technical Details | Return value: |
---|---|
An array containing the deleted elements (if any) | JavaScript Version: 1 |
More examples4in the1elements:
var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun']; months.splice(4, 1, &39; with &39;); // replacing at index39;Jun'replace &39; with &39;May 4Test and see‹/›
; at index3from index1elements:
var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun']; months.splice(3, 1);Test and see‹/›
; at index2from index2elements:
var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun']; months.splice(2, 2);Test and see‹/›
by removing from index 02elements, and insert4values:
var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun']; months.splice(0, 2, &39;A', &39;B', &39;C', &39;D');Test and see‹/›
Thesplice()The method returns an array containing the deleted elements:
var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun']; var arr = months.splice(2, 2);Test and see‹/›