Reputation: 6038
I have this kind of array:
var foo = [ { "a" : "1" }, { "b" : "2" }, { "a" : "1" } ];
I'd like to filter it to have:
var bar = [ { "a" : "1" }, { "b" : "2" }];
This is my plunker
At line 7 in plunker when i write return JSON.stringify( x );
it is good but returning string JSON.. But when i write return x;
it becomes bad and does not return Unique JSON.
Upvotes: 0
Views: 283
Reputation: 352
first you can download underscorejs then you can use the following code
var foo = [{ "a": "1" }, { "b": "2" }, { "a": "1" }];
var result = _.uniq(foo, function (obj) {
return JSON.stringify(obj);
});
refere the following url http://underscorejs.org/
Upvotes: 1
Reputation: 2423
You can use var uniqueList =_.uniq(foo, 'a');
Here is doc: https://lodash.com/docs#uniq
here is plunker: http://plnkr.co/edit/HkirGPrs3dGZMUEnEKiT?p=preview
Upvotes: 0
Reputation: 33199
You can do this by simply using uniq
, not requiring collection:
var uniqueList =_.uniq(foo, function( x ){
return JSON.stringify(x);
});
Updated plunk here: http://plnkr.co/edit/KYW6UybdiBxuvOVX8naP?p=preview
Upvotes: 2