Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cssquirrel/7ac2654f71facd9e0f48 to your computer and use it in GitHub Desktop.
Save cssquirrel/7ac2654f71facd9e0f48 to your computer and use it in GitHub Desktop.
Filter out duplicates in an array of objects using Underscore
// bananas is a complex object
bananas = _.uniq(bananas, function(banana) {
return JSON.stringify(banana);
});
// Caveat: order of properties in an object could be jumbled depending on how objects are created
// In such a case, this trick will not work. To prevent this, consider creating objects using a class-like declaration:
var BananaModel = function(data) {
var self = this;
if(data === undefined){
self.name = "";
self.datePicked = "";
} else {
self.name = data.name;
self.datePicked = data.datePicked;
}
};
var new Banana = new BananaModel({
name: "Chiquita",
datePicked: "May 6th"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment