0

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

1 Answer 1

1

You can write a filter function which behaves in the following way:

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);
Sign up to request clarification or add additional context in comments.

1 Comment

i am very new to angularjs so this is kind of throwing me off. I understand what you are trying to do but can you please tell me how .map works,does it match item with tag_id with each object, and where exactly will i be placing this code. Does this go in controllers?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.