0

in an arry of objects i want to remove object which have same id (duplicated data) using javascript.

below is the input array

const input = [
    {
         id: '1',
         name: 'first',
    },
    {  
         id: '1',
         name: 'first',
    },
    { 
         id: '2',
         name: 'second',
    },
    {
         id: '2',
         name: 'second',
    }, 
]

so as you see from above array there are duplicating data with id '1' and '2'. if there is similar id i want include only one

so the expected output is like below,

const output = [
    {
        id: '1',
        name: 'first',
    },
    {
        id: '2',
        name: 'second',
    },
]

how can i do this. could someone help me with this. i am new to programming thanks.

  • 2
    Does this answer your question? [How to remove all duplicates from an array of objects?](https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects) – Neeraj Kumar Nov 15 '22 at 08:33
  • What do you want to if `id` is the same while `name` is diffrerent? – flyingfox Nov 15 '22 at 08:33
  • @lucumt i think that data i woudnt receive. meaning the data is always the same with same id –  Nov 15 '22 at 08:34
  • So just id the same,then make them as duplicate? – flyingfox Nov 15 '22 at 08:35

5 Answers5

0

Similar to this answer. You will have to change the const to let while declaring input though, or use a new variable I suppose.

filtered_input = input.filter((value, index, self) =>
  index === self.findIndex((t) => (
    t.id === value.id
  ))
)
Gaurav Punjabi
  • 153
  • 1
  • 6
0

You can use reduce to filter data from the array based on some condition like bellow

const input = [
    {
         id: '1',
         name: 'first',
    },
    {  
         id: '1',
         name: 'first',
    },
    { 
         id: '2',
         name: 'second',
    },
    {
         id: '2',
         name: 'second',
    }, 
]

const result = input.reduce((accumulator, current) => {
  let exists = accumulator.find(item => {
    return item.id === current.id;
  });
  if(!exists) { 
    accumulator = accumulator.concat(current);
  }
  return accumulator;
}, []);

console.log(result);
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31
0

There is a lot of good approachs here. Here is my approach for removing matching property from the original array and sending it back in the return if found.

I prefer to use this one, if you are looping through a external array and matching them, this way you dont need to loop through the whole array again and again for each, because while you are finding the matches it keeps removing them from the original array, increasing performance.

Note that this will return the first match found

let id = "601985b485d9281d64056953"
    let contacts = [{
    ...,
      parent: "601985b485d9281d64056953",
    ...,
    },
    {
    ...,
      parent: "601985b485d9281d64065128",
    ...,
    }
    ]
      function findAndRemoveObjectFromArray(array, internalProperty, externalProperty, convertType = "string", returnObject = false) {
    let objIndex = -1
    if (convertType === "string") objIndex = array.findIndex((obj) => String(obj[`${internalProperty}`]) === String(externalProperty));
    if (convertType === "number") objIndex = array.findIndex((obj) => Number(obj[`${internalProperty}`]) === Number(externalProperty));

    if (objIndex > -1) {
      const object = array.splice(objIndex, 1);
      if (returnObject) return object.shift()
      return object
    }
    return [];
  }
 let currentContact = findAndRemoveObjectFromArray(contacts, "parent", id, 'string', true)
// Results:{..., parent: "601985b485d9281d64056953",...}
-1

you could use Set to get rid of the duplicate data like this

const input = [
    {
         id: '1',
         name: 'first',
    },
    {  
         id: '1',
         name: 'first',
    },
    { 
         id: '2',
         name: 'second',
    },
    {
         id: '2',
         name: 'second',
    }, 
]

const result = [...new Set(input.map(JSON.stringify))].map(JSON.parse)
console.log(result)
R4ncid
  • 6,944
  • 1
  • 4
  • 18
-1

Below is another approach

const input = [
{
     id: '1',
     name: 'first',
},
{  
     id: '1',
     name: 'first',
},
{ 
     id: '2',
     name: 'second',
},
{
     id: '2',
     name: 'second',
}, 
];
const uniqueIds = new Set();
const uniqueList = input.filter(element => {
const isDuplicate = uniqueIds.has(element.id);
  uniqueIds.add(element.id);
  return !isDuplicate;
});
console.log(uniqueList);
Amaarockz
  • 4,348
  • 2
  • 9
  • 27