Marco Standzettel
Marco Standzettel

Reputation: 3

AngularJS: JSON array from Filter

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

Upvotes: 0

Views: 75

Answers (3)

c4605
c4605

Reputation: 185

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'}}

Upvotes: 0

huan feng
huan feng

Reputation: 8623

I tried, it works perfectly

{{(items | theFilter).even}}

worked plnkr

Upvotes: 1

Christoph
Christoph

Reputation: 2053

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

Upvotes: 0

Related Questions