Wednesday, 25 July 2012

Remove specific element from a javascript array

JavaScript Array has a splice() method which can be used to remove an element or set of elements starting from a given index. First, find the index of the element you want to remove:

var array = [2, 5, 9];
var index = array.indexOf(2);
Then remove it with splice:

array.splice(index,1);

No comments:

Post a Comment