How can I remove a specific item from an array?

Created by Tom WadleyLinked to 61.7m issues across 203 teams


The splice() method can be used to modify the contents of an array by removing existing elements and/or adding new elements. To do this, first use indexOf to find the index of the array element you want to remove, and then use splice to remove that index. For example:

const array = [2, 5, 9]; console.log(array); const index = array.indexOf(5); if (index > -1) { // only splice array when item is found array.splice(index, 1); // 2nd parameter means remove one item only } // array = [2, 9] console.log(array);
We use cookies to personalize content
run ads, and analyze traffic.