1

I have 2 arrays of JavaScript objects:

arr1=[{'id':'A'},{'id':'B'}, {'id':'C'}];
arr2=[{'id':'A'},{'id':'C'}, {'id':'D'}, {'id': 'E'}];

How do I get only elements without duplicates?

Thanks.

EDIT

I dont want to remove the duplicate. I want to ONLY get the unique object.

output = [{'id':'B'}, {'id':'D'}, {'id':'E'}]

NOT

output = [{'id':'A'},{'id':'B'}, {'id':'C'}, {'id':'D'}, {'id':'E'}]

Sorry if I wasn't being clear

gyohza
  • 736
  • 5
  • 18
James Lee
  • 656
  • 1
  • 7
  • 22
  • Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Kundan Jun 04 '20 at 01:18
  • The link you provided keeps the duplicate. I want to only find the unique object – James Lee Jun 04 '20 at 01:18
  • 1
    Combine the two arrays into one and then use an answer from a previous comment to remove duplicates – Jacob Jun 04 '20 at 01:20
  • Yeah, I agree with @Jacob – Kundan Jun 04 '20 at 01:21
  • There are 93 answers on Kundan's linked question. I doubt you went through all of them in less than a minute. – Heretic Monkey Jun 04 '20 at 01:22
  • @Jacob If I combine both array and use the method, wouldn't I get all the unique keys? `A, B, C, D, E` – James Lee Jun 04 '20 at 01:26
  • @HereticMonkey The question itself is different than mine – James Lee Jun 04 '20 at 01:28
  • 1
    @JamesLee sorry I didn't actually read the linked post. I was just clarifying how you would use a method to remove duplicates from an array in your situation. Edit: Oh I get it. Disregard – Jacob Jun 04 '20 at 01:29

4 Answers4

1

arr1=[{'id':'A'},{'id':'B'}, {'id':'C'}];
arr2=[{'id':'A'},{'id':'C'}, {'id':'D'}, {'id': 'E'}];

const out = Object.entries(arr1.concat(arr2).reduce((a, c) => {
  a[c.id] = a[c.id] || 0;
  a[c.id]++;
  return a;
}, {})).filter(([k, v]) => v === 1).map(([k]) => ({id: k}));

console.log(out);
Chase
  • 3,028
  • 14
  • 15
1

This question is asked a lot, but in that particular case it can be solved like this:

Okay, so maybe not. Here's one way to retrieve the output like you asked:

arr1=[{'id':'A'},{'id':'B'}, {'id':'C'}];
arr2=[{'id':'A'},{'id':'C'}, {'id':'D'}, {'id': 'E'}];

function getUniques(arr) {
  let counter = arr.reduce((acc, val) =>
    (acc[val.id] = (acc[val.id] || 0) + 1, acc), {});
  return Object.keys(counter)
    .filter(k => counter[k] === 1)
    .map(k => ({id: k}));
}

console.log(getUniques([...arr1, ...arr2]));
gyohza
  • 736
  • 5
  • 18
1

A little messy but it works. First find ids that only occur once, then filter combined arrays to only include unique ids.

arr1 = [{ id: "A" }, { id: "B" }, { id: "C" }];
arr2 = [{ id: "A" }, { id: "C" }, { id: "D" }, { id: "E" }];

function getCount(array, elem) {
    return array.filter((e) => e === elem).length;
}

uniqueIds = [...arr1, ...arr2]
    .map((e) => e.id)
    .filter((e, i, a) => getCount(a, e) === 1);

let uniques = [...arr1, ...arr2].filter((e) => uniqueIds.includes(e.id));

console.log(uniques);

Jacob
  • 155
  • 8
0

Just another approach :

 arr1=[{'id':'A'},{'id':'B'}, {'id':'C'}];
 arr2=[{'id':'A'},{'id':'C'}, {'id':'D'}, {'id': 'E'}];
 var res = [];
 var newArr = arr1.concat(arr2);
 var reverseArr = newArr.slice().reverse();
 newArr.forEach((el,idx)=> {
 let iex = newArr.findIndex((e,i)=> e.id == el.id );
 if (iex === idx && iex === newArr.length - (reverseArr.findIndex((e,i)=> e.id == 
   el.id )+ 1) ){
     res.push(el)
   }
});
console.log(res) 
shshsi
  • 106
  • 3