简体   繁体   中英

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

I tried, it works perfectly

{{(items | theFilter).even}}

worked plnkr

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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