简体   繁体   English

来自包含对象的数组的angularjs过滤器

[英]angularjs filter from array that contains object

This might be simple but i am not sure how to go about it. 这可能很简单,但我不确定如何去做。 I have created two scope array(not sure if required in controller) and my actual list in controller 我创建了两个作用域数组(不确定是否在控制器中需要)和我的实际列表在控制器中

$scope.tagFilter = [{tag_id:1,tag_name:test},{tag_id:2,tag_name:test2}];
$scope.categoryFilter = [{cat_id:1,cat_name:test3},{cat_id:2,cat_name:test4}]

my actual list 我的实际清单

$scope.list = [{list_id:1,category_id:1,tag:1},...]

Is it possible to create a filter where i can compare tag_id with tag in list and cat_id with category_id I was thinking of creating angular.module.filter but not really sure how to do it 是否有可能创建一个过滤器,在其中我可以将tag_id与list中的标记和cat_id与category_id进行比较,我当时正在考虑创建angular.module.filter但不确定如何做

You can write a filter function which behaves in the following way: 您可以编写一个filter函数,该函数的行为如下:

var allowed_tags = $scope.tagFilter.map(function(item){
    return item.tag_id;
})

var allowed_cat = $scope.categoryFilter.map(function(item){
    return item.cat_id;
})

var filtered = $scope.list.filter(function(i){
return ((allowed_tags.indexOf(i.tag) != -1) && 
        (allowed_cat.indexOf(i.category_id) != -1));
})

console.log(filtered);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM