Created
August 12, 2013 21:48
-
-
Save datchley/6215614 to your computer and use it in GitHub Desktop.
Remove duplicates from an array of objects in javascript using a function generator.
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
var data = [{ | |
author: "Stephen King", | |
title: "It" | |
}, { | |
author: "Anne Rice", | |
title: "Exit to Eden" | |
}, { | |
author: "Stephen King", | |
title: "It" | |
}, { | |
author: "Carl Sagan", | |
title: "Contact" | |
}]; | |
var dedup = function (proplist) { | |
var found = [], | |
props = proplist; | |
return function _dedup(elm) { | |
var key = ""; | |
$(props).each(function(i,prop) { key += elm[prop]; }); | |
if (found.indexOf(key) == -1) { | |
found.push(key); | |
return true; | |
} | |
return false; | |
} | |
}; | |
var unique_results = data.filter(dedup(['title', 'author'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment