I have an object that contains an array of objects.
things = new Object();
things.thing = new Array();
things.thing.push({place:"here",name:"stuff"});
things.thing.push({place:"there",name:"morestuff"});
things.thing.push({place:"there",name:"morestuff"});
I'm wondering what is the best method to remove duplicate objects from an array. So for example, things.thing would become…
{place:"here",name:"stuff"},
{place:"there",name:"morestuff"}
Best Answer
How about with some
es6
magic?Reference URL
A more generic solution would be:
Using the above property strategy instead of
JSON.stringify
:You can add a wrapper if you want the
propNames
property to be either an array or a value:allowing both
getUniqueItemsByProperties('a')
andgetUniqueItemsByProperties(['a']);
Stackblitz Example
Explanation