Ask Your Question
3

How to remove an element with a specific value from an array containing multiple objects?

asked 2023-02-16 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-10-17 18:00:00 +0000

huitzilopochtli gravatar image

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'}
]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-16 11:00:00 +0000

Seen: 11 times

Last updated: Oct 17 '21