0

i have a quick question. I have a filter where i pass an array and get back an Array:

The Filter returns a JSON Array generated by underscore.js library:

myApp.filter('theFilter', function () {
        return function(items){
            return _.countBy(items, function(num) {
              return num % 2 == 0 ? 'even': 'odd';
            });
        };
});

this {{array | theFilter }} outputs only a json array like this: {{ "even":3, "odd":5 }}

How can i output the value of even for example?

Thanks and best regards

2

3 Answers 3

1

I tried, it works perfectly

{{(items | theFilter).even}}

worked plnkr

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

Comments

0
myApp.filter('theFilter', function () {
    return function(items){
        return _.countBy(items, function(num) {
          return num % 2 == 0 ? 'even': 'odd';
        }).even;
    };
});

Comments

0

I think maybe you need write another filter:

myApp.filter('theFilter', function () {
        return function(items){
            return _.countBy(items, function(num) {
              return num % 2 == 0 ? 'even': 'odd';
            });
        };
});

myApp.filter('attr', function () {
        return function(obj, attrName){
            return obj ? obj[attrName] : undefined;
        };
});

{{array | theFilter | attr:'even'}}

2 Comments

@MarcoStandzettel plnkr.co/edit/oNK3v7CHhOKlXvXXZYju?p=preview you can have a try
But I think the solution of @huan-feng is better :)

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.