Skip to content

Instantly share code, notes, and snippets.

@datchley
Created August 12, 2013 21:48
Show Gist options
  • Save datchley/6215614 to your computer and use it in GitHub Desktop.
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.
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