Suppose I have following arrays of objects
var firstDataSet = [
{'id': 123, 'name': 'ABC'},
{'id': 456, 'name': 'DEF'},
{'id': 789, 'name': 'GHI'},
{'id': 101, 'name': 'JKL'}
];
var secondDataSet = [
{'id': 123, 'name': 'ABC', 'xProp': '1q'},
{'id': 156, 'name': 'MNO', 'xProp': '2w'},
{'id': 789, 'name': 'GHI', 'xProp': '3e'},
{'id': 111, 'name': 'PQR', 'xProp': '4r'}
];
Now I want to collect array with unique objects (matching id
and name
)i.e.
var firstDataSet = [
{'id': 123, 'name': 'ABC', 'xProp': '1q'},
{'id': 456, 'name': 'DEF'},
{'id': 789, 'name': 'GHI', 'xProp': '3e'},
{'id': 101, 'name': 'JKL'},
{'id': 156, 'name': 'MNO', 'xProp': '2w'},
{'id': 111, 'name': 'PQR', 'xProp': '4r'}
];
I am able to collect ALL with
Array.prototype.unshift.apply(firstDataSet , secondDataSet );
But not sure how I can filter out duplicates. Any suggestion?
Edit: My object on two different array are not same. At least based on number of properties.
To remove the duplicates from an array of objects:Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.
This can be achieved By extending Set
class Like below
var firstDataSet = [
{'id': 123, 'name': 'ABC'},
{'id': 456, 'name': 'DEF'},
{'id': 789, 'name': 'GHI'},
{'id': 101, 'name': 'JKL'}
];
var secondDataSet = [
{'id': 123, 'name': 'ABC', 'xProp': '1q'},
{'id': 156, 'name': 'MNO', 'xProp': '2w'},
{'id': 789, 'name': 'GHI', 'xProp': '3e'},
{'id': 111, 'name': 'PQR', 'xProp': '4r'}
];
Array.prototype.unshift.apply(firstDataSet , secondDataSet );
//console.log(firstDataSet)
class UniqueSet extends Set {
constructor(values) {
super(values);
const data = [];
for (let value of this) {
if (data.includes(JSON.parse(value.id))) {
this.delete(value);
} else {
data.push(value.id);
}
}
}
}
console.log(new UniqueSet(firstDataSet))
Working link
This was the original question.
Use a Set
:
The Set object lets you store unique values of any type, whether primitive values or object references.
You can also use object literals.
var list = [JSON.stringify({id: 123, 'name': 'ABC'}), JSON.stringify({id: 123, 'name': 'ABC'})];
var unique_list = new Set(list); // returns Set {"{'id': 123, 'name': 'ABC'}"}
var list = Array.from(unique_list); // converts back to an array, and you can unstringify the results accordingly.
For more ways to construct a set back to an array, you can follow instructions here.
If you can't use ES6 (which is what defines Set
), there's a polyfill for older browsers.
Unfortunately, these objects are no longer strictly duplicates and cannot be tackled in a friendly way using Set
, for instance.
The easiest way to approach this type of problem is to iterate through the array of objects, identify those with repeated property values, and eliminate in place using splice
, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With