In AngularJS, the $scope.categories array is populated from a multi-select element.
$scope.categories = ["Adventure", "Strategy"]
To compare this array with the categories in the items array below:
$scope.items = [
{
title: "Star Wars",
categories: ["Adventure"]
}, {
title: "Star Search",
categories: ["Adventure", "Strategy"]
}, {
title: "Star Trek",
categories: ["Adventure", "Family"]
}, {
title: "Star Wars",
categories: ["Family", "Strategy"]
}];
The values in $scope.categories must match those in $scope.items.categories for an object to be pushed to an output array.
The resulting $scope.filtered array will be (items1):
{
title: "Star Search",
categories: ["Adventure", "Strategy"]
}
I have the logic up until the point where the loop needs to iterate again... how can I achieve this?
- I loop through $scope.categories
- I then loop through $scope.items
- I then loop through each object's $scope.item.categories array.
I compare the value of $scope.categories with the value of $scope.item.categories
for (var i = 0; i < categories.length; i++) { for (var j = 0; j < items.length; j++) { for (var k = 0; k < items[j].categories.length; k++) { if(categories[i] == items[j].categories[k]) { console.log("The value of " + categories[i] + ", matches " + items[j].categories[k]); } else { console.log("The value of " + categories[i] + ", is not a match"); } } } }
Check out this JSbin example!