The $scope.categories array is being populated from a multi-select element in AngularJS.
$scope.categories = ["Adventure", "Strategy"]
I need to compare this array against the categories array 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"]
}];
Both values in $scope.categories need to match the same values in $scope.items.categories, for the object to be pushed to an output array.
With the resulting $scope.filtered array being (items1):
{
title: "Star Search",
categories: ["Adventure", "Strategy"]
}
I have the logic until the loop needs to reiterate... but how?
- I am looping through $scope.categories
- Then looping through $scope.items
- Then looping through $scope.item.categories array of each object.
Then I am comparing the $scope.categories value against 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"); } } } }
Here is a JSbin example
$scopestaff... just an opinion ;)