Basically splice() method is used to change the content of an array by removing existing element and add new elements . It contain three parameter start , deletecount and ItemN.
start ->It is the position where to start the selection .And if you want to select element from end then you can use negative number like (1 , -3) .
deletecount ->The position indicating which element want to remove from an array . If deletecount is 0 then no elements are removed .If the length of deletecount is greater than the length of exisiting element of original array , then all of the elements through the end of the array will be deleted .
ItemN ->This position indicate which element you want to add in an original array . If you don't specify this position then splice() will remove the elements only .
Return :
In an array deleted elements will be store .If only one element is remove ,then only one element is returned by an array .If no element is removed then no element will be returned.
Example :
var names = [' Mukesh ' , ' Ravi ' , 'Ayush ' , ' Pranav ' ,' Mukesh ' , ' Ishan '];<br>
var rem = names.splice('1' , '0' , 'Abhishek ' ) //**remove 0 elements from index 1 and insert Abhishek**<br>
**output ->** names = [' Mukesh ' , ' Abhishek ', ' Ravi ' , 'Ayush ' , ' Pranav ' ,' Mukesh ' , ' Ishan ' ] <br>
Removed element is [ ],means no elements is removed <br><br>
var names = [' Mukesh ' , ' Ravi ' , 'Ayush ' , ' Pranav ' ,' Mukesh ' , ' Ishan '];<br>
var rem = names.splice('2' , '1' )//**remove 1 element from index 2**<br>
**output ->** names = [' Mukesh ' , ' Abhishek ', 'Ayush ' , ' Pranav ' ,' Mukesh ' , ' Ishan ' ] <br>
Removed element is [' Ravi ' ],means 1 elements is removed <br><br>
var names = [' Mukesh ' , ' Ravi ' , 'Ayush ' , ' Pranav ' ,' Mukesh ' , ' Ishan '];<br>
var rem = names.splice('0' , '2' , 'Medha' ,'Anurag' ,'Seema' )//**remove 2 element from index 0 and insert 'Medha' , 'Anurag' , 'Seema '**<br>
**output ->** names = [' Medha ' , ' Anurag ', 'Seema' 'Ayush ' , ' Pranav ' ,' Mukesh ' , ' Ishan ' ] <br>
Removed element is [' Mukesh ' , 'Ravi' ],means 2 elements is removed <br>
1 Comment(s)