How to Remove an Object from an Array by its Value in JavaScript?

Estimated read time 1 min read

To remove an object from an array by its value in JavaScript, you can use the filter() method. Here’s an example:

let arr = [{id: 1, name: "John"}, {id: 2, name: "Jane"}, {id: 3, name: "Jim"}];
let newArr = arr.filter(obj => obj.id !== 2);
console.log(newArr);

This code removes the object {id: 2, name: "Jane"} from the array [{id: 1, name: "John"}, {id: 2, name: "Jane"}, {id: 3, name: "Jim"}] and logs the modified array: [{id: 1, name: "John"}, {id: 3, name: "Jim"}].

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply