I am currently working on developing an API, where I need to make a function to extract "completely" unique objects, i.e. no attribute match in any of the objects.
I have an array of objects of fixed schema like: (Modified this)
let arr = [{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1",
"e": "e1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2",
"e": "e2"
},
{
"a": "a3",
"b": "b3",
"c": "c2", // matching attribute
"d": "d3",
"e": "e3"
},
// values in previous object should not affect this unique object:
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3",
"e": "e3"
},
// … more objects here, maximum 200 * 33 objects
]
And I want the result as:
[
{
"a": "a1",
"b": "b1",
"c": "c1",
"d": "d1",
"e": "e1"
},
{
"a": "a2",
"b": "b2",
"c": "c2",
"d": "d2",
"e": "e2"
},
{
"a": "a3",
"b": "b3",
"c": "c3",
"d": "d3",
"e": "e3"
},
// …
]
In case of matching attribute, the object which is encountered first in the array shall be kept and the other be discarded. Different properties will never have overlapping values.
My current implementation (wrong, see comments):
for (let i in arr[0]) {
let map = new Map();
arr = arr.filter(obj => map.get(obj[i]) ? false : map.set(obj[i], true));
}
console.log(arr);
I̶s̶ ̶t̶h̶e̶r̶e̶ ̶a̶n̶y̶ ̶b̶e̶t̶t̶e̶r̶ ̶(̶m̶o̶r̶e̶ ̶e̶f̶f̶i̶c̶i̶e̶n̶t̶)̶ What is the correct way to do this?