Reputation: 1224
I have an array of objects and I would like to remove duplicate items. The following discusses the topic however the ES6 solutions do not appear to work with objects: Unique values in an array
var myArray=[{fname:"Bob", sname:"Brown"},{fname:"Jill",sname:"White"},{fname:"Bob", sname:"Brown"}];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
/*
[ { fname: 'Bob', sname: 'Brown' },
{ fname: 'Jill', sname: 'White' },
{ fname: 'Bob', sname: 'Brown' } ]
*/
let unique = [...new Set(myArray)];
/*
[ { fname: 'Bob', sname: 'Brown' },
{ fname: 'Jill', sname: 'White' },
{ fname: 'Bob', sname: 'Brown' } ]
*/
Upvotes: 1
Views: 2069
Reputation: 339
Use _.uniqBy or _.uniqWith from lodash ;)
const _ = require('lodash');
const myArray=[{fname:"Bob", sname:"Brown"},{fname:"Jill",sname:"White"},{fname:"Bob", sname:"Brown"}];
console.log(_.uniqWith(myArray, _.isEqual));
Upvotes: 5
Reputation: 137
Here first and last element does not represent same object, therefore it is not removed from set
var myArray=[{fname:"Bob", sname:"Brown"},{fname:"Jill",sname:"White"},{fname:"Bob", sname:"Brown"}];
var set1 = new Set(myArray);
console.log([...set1]);
/*
[ { fname: 'Bob', sname: 'Brown' },
{ fname: 'Jill', sname: 'White' },
{ fname: 'Bob', sname: 'Brown' } ]
*/
Here only one object is added to set, as both are same.
var set2 = new Set();
var obj1 = {fname:"Bob", sname:"Brown"};
set2.add(obj1);
set2.add(obj1);
console.log([...set2]);
/*
[ { fname: 'Bob', sname: 'Brown' }]
*/
You have to compare manually both object content, and removed any duplicates.
Upvotes: -1
Reputation: 174
Without lodash:
function removeDuplicates(myArr, prop) {
return myArr.filter((element, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(element[prop]) === pos;
});
}
Upvotes: 0
Reputation: 111278
With lodash you can use _.uniqBy
or _.uniqWith
- see the answer by Steven Leclerc.
See: https://lodash.com/ for details.
If you're serious about writing code then you shouldn't reinvent the wheel for simple cases like this.
If you want to know how to implement it properly, see the source code here:
Keep in mind that when you use JSON.stringify then first of all you must put it inside of a try/catch block because your program will crash for some data (or you should use tryjson - disclaimer, I'm the author of that module) and that you will get false positives of objects that are different but have the same JSON serialization.
Upvotes: 2
Reputation: 2828
A solution would be to use the same solution you referred to after a small modification; as using Objects as keys is not possible, we can use them as JSON which is string:
Array.prototype.getUnique = function() {
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i) {
var index = JSON.stringify(this[i]);
if(u.hasOwnProperty(index)) {
continue;
}
a.push(this[i]);
u[index] = 1;
}
return a;
}
Hope this helps :)
Upvotes: 0