$scope.categories数组是从AngularJS的多选元素中填充的。
$scope.categories = ["Adventure", "Strategy"]
我需要将此数组与下面项目中的类别数组进行比较:
$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"]
}];
为了将对象推入输出数组,$scope.categories中的两个值需要与$scope.items.categories中的相同值匹配。
结果为$scope.filtered数组(items1):
{
title: "Star Search",
categories: ["Adventure", "Strategy"]
}
我已经掌握了循环的逻辑...但如何进行迭代?
- 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"); } } } }
这里是一个JSbin例子
$scope
等内容感到困惑...这只是个人意见 ;) - Kutyel