How to remove a particular element from an array in JavaScript? Find the index
of the array element you want to remove using indexOf
, and then remove that index with splice. The splice() method changes the contents of an array by removing existing elements and/or adding new elements.
1 2 3 4 5 6 7 8 9 10 11 |
const array = [2, 5, 9]; console.log(array); const index = array.indexOf(5); if (index > -1) { array.splice(index, 1); } // array = [2, 9] console.log(array); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.