1 | initial version |
To remove an element with a specific value from an array containing multiple objects, you can use the filter() method. The filter() method creates a new array with all elements that pass the test implemented by the provided function.
For example, let's say you have an array of objects with the following structure:
let items = [
{id: 1, name: 'Item 1'},
{id: 2, name: 'Item 2'},
{id: 3, name: 'Item 3'},
{id: 4, name: 'Item 4'}
];
To remove an object with id
equal to 3
, you can use the filter() method as follows:
items = items.filter(item => item.id !== 3);
This will create a new array that includes all objects in the original array except for the one with id
equal to 3
. The updated items
array will be:
[
{id: 1, name: 'Item 1'},
{id: 2, name: 'Item 2'},
{id: 4, name: 'Item 4'}
]