skip to Main Content

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


  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);
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.

  3. 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);
    
    Login or Signup to reply.
  4. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search