How to remove a specific element from an array in javascript
I have a requirement to delete a particular element from an array. Is there any direct function to remove a specific element like .push() method is to insert an element?
1 Answer
6 years ago by Anusha
I can think of the below two ways to remove a specific element from an array. check below options and i hope this meets your requirement.
// Removing element using filter method
let x = 5;
let array = [1, 2, 3, 4, 5, 6, 7, 8, 5];
array = array.filter(ele => ele !== x);
console.log(array);
// Removing element based on Index using splice method
let index = array.indexOf(7);
if (index > -1) {
array.splice(index, 1);
}
console.log(array);
6 years ago by Divya