Showing posts with label JavaScript Arrays. Show all posts
Showing posts with label JavaScript Arrays. Show all posts

How to Remove a Specific Item From an Array in JavaScript

Quick answer: find the index with indexOf, then remove it with splice:

const array = [2, 5, 9];
const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1); // removes 1 element at that index
}
// array is now [2, 9]