小编Joh*_*eri的帖子

针对所有另一个数组过滤数组

$ scope.categories阵列正被从在AngularJS多选择元件填充.

$scope.categories = ["Adventure", "Strategy"]
Run Code Online (Sandbox Code Playgroud)

我需要将此数组与下面的items数组中的categories数组进行比较:

$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"]
}];
Run Code Online (Sandbox Code Playgroud)

在这两个值$ scope.categories需要匹配在相同的值$ scope.items.categories,对象被推向一个输出阵列.

生成的$ scope.filtered数组为(项1):

{
  title: "Star Search",
  categories: ["Adventure", "Strategy"]
}
Run Code Online (Sandbox Code Playgroud)

我有逻辑,直到循环需要重申......但是如何?

  1. 我循环遍历$ scope.categories
  2. 然后循环遍历$ scope.items
  3. 然后循环遍历每个对象的$ scope.item.categories数组.
  4. 然后我比较$ scope.categories对价值价值$ scope.item.categories

    for …
    Run Code Online (Sandbox Code Playgroud)

javascript arrays loops for-loop angularjs

3
推荐指数
1
解决办法
83
查看次数

标签 统计

angularjs ×1

arrays ×1

for-loop ×1

javascript ×1

loops ×1