0

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

1
  • What code have you tried? You need a function that creates another list without the things you don't want. Commented Nov 24, 2022 at 5:07

4 Answers 4

2

const uniqueIds = [];

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

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

    return true;
  }

    return false;
  });

  // 👇️ 
  console.log(unique);
Sign up to request clarification or add additional context in comments.

Comments

0

There are several methods for obtaining a filtered object.

    { 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", }
];

Option first

const itemUIds = items.map(o => o.id)
const filteredOption1 = items.filter(({id}, index) => !itemUIds.includes(id, index + 1));
console.log(filteredOption1)

Option second

const filteredOption2 = Object.values(items.reduce((acc,cur)=>Object.assign(acc,{[cur.id]:cur}),{}));
console.log(filteredOption2)

Option third

const uniqueItem:any = {};
const filteredOption3 = items.filter(obj => !uniqueItem[obj.id] && (uniqueItem[obj.id] = true));
console.log(filteredOption3);

Comments

0

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.

newArr = [...new Map(array.map(item => [item["id"], item])).values()]// I am passing `id` as key

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"}]

newArr = [...new Map(array.map(item => [item["id"], item])).values()]

console.log(newArr);

Comments

-1

You can filter an array to make a copy without the unwanted elements:

const 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" }
];

// only items where id is not 2
const filtered = array.filter(item => item.id !== 2);

console.log(filtered);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.