Remove Duplicate Objects From Javascript Array (Lodash)
HTML
<div>
<pre class="float-left font-weight-600">Before</pre>
<pre class="float-right font-weight-600">After</pre>
</div>
<pre id="before" class="float-left"></pre>
<pre id="after" class="float-right"></pre>
CSS
@import url(http://fonts.googleapis.com/css?family=Source+Code+Pro:300,600);
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,600);
body {
font-family: 'Source Sans Pro', sans-serif;
}
pre {
width: 50%;
display: inline;
font-family: 'Source Code Pro';
font-weight: 300;
font-size: 13px;
text-overflow: ellipsis;
overflow: hidden;
}
.float-left {
float: left;
}
.float-right {
float: right;
}
.font-weight-600 {
font-weight: 600;
}
.clear-both {
clear: both;
}
JavaScript
var arrayOfObj = [
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
},
{
"name": "Paul",
"id": "2",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
},
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
}
];
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);