Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active February 17, 2016 19:22
Show Gist options
  • Save lsloan/cbb313994822864521fc to your computer and use it in GitHub Desktop.
Save lsloan/cbb313994822864521fc to your computer and use it in GitHub Desktop.
A very simple filter intended for use with AngularJS's `ng-repeat`, etc. to use only unique array items.

I wrote this for a project that didn't want to import additional libraries just for this functionality.
It could probably be more efficient and it only supports direct attributes of the item in question.
It would be nice to add support for attr.subattr.etc at some point. (Maybe using AngularJS's $parse()?)

Other similar functions:

angular.module('myFilters', [])
.filter('unique', function () {
return function (items, attr) {
var seen = {};
return items.filter(function (item) {
return (angular.isUndefined(attr) || !item.hasOwnProperty(attr))
? true
: seen[item[attr]] = !seen[item[attr]];
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment