Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using splice() to Remove Elements in Javascript

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 119
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: