Created
May 6, 2015 19:04
-
-
Save cssquirrel/7ac2654f71facd9e0f48 to your computer and use it in GitHub Desktop.
Filter out duplicates in an array of objects using Underscore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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