This Angular filter is being passed an array of objects used in an ng-repeat (item in items).
[{
title: "Star Wars",
categories: ["Adventure", "Family"]
}, {
title: "Star Search",
categories: ["Realty", "Fantasy"]
}, {
title: "Star Trek",
categories: ["Sci-Fi", "Fantasy"]
}]
And an array of strings (categoriesFiltered)
["Adventure", "Sci-Fi"]
What would be the best way to iterate through the items in the ng-repeat and compare the categories array... to the categoriesFiltered array, and push the items that match to the new filtered array?
This is how far I have come
categoryFilter.filter('filterCategories', function () {
return function (items, categoriesFiltered) {
var filtered = [];
for (var i = 0; i < items.length; i++) {
for (var j = 0; j < items[i].categories; j++) {
if (filtered.indexOf(items[i].categories[j]) === -1) {
filtered.push(items[i]);
};
};
};
return filtered;
};
});
Here is a plunker...
Array.prototype.filter()
orArray.prototype.map()
– charlietfl Jul 27 at 0:56