Hello i am new to programming and i have a problem on system in which i am trying to make a Booking system.
I want to remove a chunk of objects with same id from an array while clicking a btn.
Here is an array..
array = [
{ id: 1, futsal: "4", time: "06:00 - 09:00", … },
{ id: 1, futsal: "4", time: "06:00 - 09:00", … },
{ id: 1, futsal: "4", time: "06:00 - 09:00", … },
{ id: 2, futsal: "4", time: "07:00 - 08:00", … },
{ id: 2, futsal: "4", time: "07:00 - 07:00", … },
{ id: 3, futsal: "4", time: "08:00 - 09:00", … },
{ id: 3, futsal: "4", time: "08:00 - 09:00", … },
{ id: 3, futsal: "4", time: "08:00 - 09:00", … }]
I want to remove all the objects with same id at once i.e either all objects with id=1 or 2…
4
Answers
You can filter an array to make a copy without the unwanted elements:
There are several methods for obtaining a filtered object.
Option first
Option second
Option third
If you are going to check with
id
as a unique key then you can use the ES6 feature to get a unique list of objects by key.