0

How can we delete element from array?

28th Apr 2016, 2:10 PM
Asfandyar khan
Asfandyar khan - avatar
2 Réponses
+ 1
First, find the index of the element you want to remove: var array = [2, 5, 9]; var index = array.indexOf(5); Note: browser support for indexOf is limited; it is not supported in Internet Explorer 7 and 8. Then remove it with splice: if (index > -1) { array.splice(index, 1); } The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.
8th Jun 2016, 7:35 PM
Angel Luna
Angel Luna - avatar
0
//remove element at the end array.pop(); //remove element at the start array.shift(); // remove data by index var index = 1; array.splice(index,1); console.log(array);
10th Jul 2016, 4:56 AM
Sumanth Reddy
Sumanth Reddy - avatar