// the original array var array = ["One", "one", "four"]; // Splice (position, Numberofitemstoremove, item) // stitching function (index position, number of elements to delete, Element)Array.splice (2, 0, "three"); array; // now the array is like this ["one", "Three ", "four"]
function (Index, item) { this. Splice (index,0, item);}; You can do this later with var nums = ["One", "one", "one", "four"];nums.insert (// Note Array index, [0,1,2 ...] Array // ["One", "one", "three", "four"]
The instance extracts two elements from position 2 and adds a new element to the 2nd position in the array: var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice (2,2); Fruits output: Banana, Orange
Parameter values
Parameters |
Description |
Index |
Necessary. Specifies where to add/remove elements from. This parameter is the subscript that begins inserting and/or deleting an array element, which must be a number. |
Howmany |
Necessary. Specifies how many elements should be deleted. Must be a number, but it can be "0". If this parameter is not specified, all elements from index start to the end of the original array are deleted. |
item1, ..., ItemX |
Optional. The new element to add to the array |
return value
Type |
Description |
Array |
If the element is removed from the Arrayobject, the array containing the deleted element is returned. |
JavaScript Array Splice function