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.