Home > OS >  Remove all the objects with same value on javascript
Remove all the objects with same value on javascript

Time:11-24

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...

CodePudding user response:


const uniqueIds = [];

  const unique = arr.filter(element => {
  const isDuplicate = uniqueIds.includes(element.id);

  if (!isDuplicate) {
    uniqueIds.push(element.id);

    return true;
  }

    return false;
  });

  //            
  • Related