1

I have a array of categories object

categoriesTree : [
    {name: 'cat1', id:1, selected : true},
    {name: 'cat2', id:2, selected : false},
    {name: 'cat3', id:3, selected : true},
    {name: 'cat4', id:4, selected : false},
    {name: 'cat6', id:6, selected : true},
]

To get the selected categories I did:

selectedCategories = filterFilter($scope.categoriesTree, {selected:true})

this will give me:

categoriesTree : [
    {name: 'cat1', id:1, selected : true},
    {name: 'cat3', id:3, selected : true},
    {name: 'cat6', id:6, selected : true},
]

But to reduce the POST data to send, I just want the id attribue so I want an array like this:

['1', '3', '6']

Is that possible to do with AngularJS filters ?

3 Answers 3

2
res = categoriesTree.map(function(el) { return el.id; });

Short and simple, with the good ole js.

To answer your question: no, with filters you can't, but it is possible with forEach

Sign up to request clarification or add additional context in comments.

Comments

0

You can create your own custom angularjs filter that not only "filters" but also "projects" your set into a different set. I think you can definitely project each category object into just its ID.

See here for an example of how to create a custom filter in angularjs.

Comments

0

you can use underscorejs's _.pluck method.

selectedCategories = _.pluck(_.filter(categoriesTree, function(category) {
    return category.selected;
}), 'id');

which returns an array of the values of all the id's where the corresponding selected value is true.

Comments

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.