Removing duplicate objects with Underscore for Javascript Removing duplicate objects with Underscore for Javascript javascript javascript

Removing duplicate objects with Underscore for Javascript


.uniq/.unique accepts a callback

var list = [{a:1,b:5},{a:1,c:5},{a:2},{a:3},{a:4},{a:3},{a:2}];var uniqueList = _.uniq(list, function(item, key, a) {     return item.a;});// uniqueList = [Object {a=1, b=5}, Object {a=2}, Object {a=3}, Object {a=4}]

Notes:

  1. Callback return value used for comparison
  2. First comparison object with unique return value used as unique
  3. underscorejs.org demonstrates no callback usage
  4. lodash.com shows usage

Another example : using the callback to extract car makes, colors from a list


If you're looking to remove duplicates based on an id you could do something like this:

var res = [  {id: 1, content: 'heeey'},  {id: 2, content: 'woah'},   {id: 1, content:'foo'},  {id: 1, content: 'heeey'},];var uniques = _.map(_.groupBy(res,function(doc){  return doc.id;}),function(grouped){  return grouped[0];});//uniques//[{id: 1, content: 'heeey'},{id: 2, content: 'woah'}]


Implementation of Shiplu's answer.

var foo = [ { "a" : "1" }, { "b" : "2" }, { "a" : "1" } ];var x = _.uniq( _.collect( foo, function( x ){    return JSON.stringify( x );}));console.log( x ); // returns [ { "a" : "1" }, { "b" : "2" } ]