There are multiple ways to remove a specific item from an array in JavaScript, some of the most common methods are:

Using the splice() method: The splice() method can be used to remove an element from an array at a specific index.

let arr = [1, 2, 3, 4, 5];
let index = arr.indexOf(3); // returns 2
arr.splice(index, 1);
console.log(arr); // [1, 2, 4, 5]

Using the filter() method: The filter() method creates a new array with all elements that pass the test implemented by the provided function.

let arr = [1, 2, 3, 4, 5];
let filteredArr = arr.filter(item => item !== 3);
console.log(filteredArr); // [1, 2, 4, 5]

Using the slice() method: The slice() method can be used to return a shallow copy of a portion of an array into a new array object.

let arr = [1, 2, 3, 4, 5];
let index = arr.indexOf(3); // returns 2
let newArr = [...arr.slice(0, index), ...arr.slice(index + 1)];
console.log(newArr); // [1, 2, 4, 5]

Using the spread operator: The spread operator ‘…’ can also be used to remove a specific item from an array.

let arr = [1, 2, 3, 4, 5];
let newArr = [...arr.filter(item => item !== 3)];
console.log(newArr); // [1, 2, 4, 5]

Note that the above examples are used to remove one specific item from the array. If you want to remove multiple items, you can use the same methods with a loop to remove the specific items one by one.

Also Read:

Categorized in: