By 05/02/2014 3 Comments

How to Remove elements of an array by their index in Javascript

Remove an array element(s) by value

  • We can remove array element by given an array index

splice() function

  • This function adds / removes elements from an array
  • This function effects to original array and also change it

Following code snippet will guide you for remove elements of an array by index in Javascript


function removeByIndex(arr, index) { // function with 2 parameters, first is an array, second is array index (to be removed)
arr.splice(index, 1); // splice() function called by array pass 2 parameters, first is an array index (to be removed), second is no of elements (to be deleted from an array)

arr= new Array(); // declare and initialize an array object
arr[0] = 'Apple'; 
arr[1] = 'Ball';
arr[2] = 'Cat';  // assign different values in array index
arr[3] = 'Dog';

alert("Array before removing elements: "+arr); // display output in an alert popup

removeByIndex(arr, 2); // call above function, pass 2 parameters, first is an array and second is array index (to be removed)

alert("Array after removing elements: "+arr);


Posted in: JavaScript

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .