Filter array of objects lodash
by Rowan Hamilton
HTML
Before:
<pre id="before" class="float-left"></pre>
After:
<pre id="after" class="float-right"></pre>
JavaScript
const arrayOfObj = [
{
value: 1,
description: "Joel is cool"
}, {
value: 2,
description: "POOP"
}, {
value: 1,
description: "Joel is cool"
}, {
value: 1,
description: "Joel is cool"
}, {
value: 2,
description: "POOP"
}, {
value: 3,
description: "Ada is a dog"
}, {
value: 2,
description: "POOP"
}, {
value: 3,
description: "Ada is a dog"
}, {
value: 1,
description: "Joel is cool"
}, {
value: 3,
description: "Ada is a dog"
}, {
value: 1,
description: "Something else"
}
];
var arrayOfObjAfter = _.map(
_.uniq(
_.map(arrayOfObj, function(obj){
return JSON.stringify(obj);
})
), function(obj) {
return JSON.parse(obj);
}
);
document.getElementById("before").innerHTML = JSON.stringify(arrayOfObj, null, 2);
document.getElementById("after").innerHTML = JSON.stringify(arrayOfObjAfter, null, 2);