How to remove object from array?

var arr = [
{id: 7, name: '2'},
{id: 1, name: '1'},
{id: 4, name: '2'},
{id: 2, name: '2'},
{id: 3, name: '3'}
]

I need a function that will delete the object with selected id
March 23rd 20 at 19:42
1 answer
March 23rd 20 at 19:44
Solution
const index = arr.findIndex(n => n.id === id);
if (index !== -1) {
 arr.splice(index, 1);
}

or if you need a new array:

arr = arr.filter(n => n.id !== id)

Find more questions by tags JavaScript