简体   繁体   中英

return object length which includes key is true not false

I want to write some filter logic where an object returns its length which includes only key having true value.

     $scope.generated.codesWithBalance = [A:true, B:true, C:false];

so for above object it should return length as 2. Since C is false so want to exclude in the count.

But now whenever i try to get the length it returns total length

    Object.keys($scope.generated.codesWithBalance).length

Any way that i can avoid key having false value?

Use Array.prototype.filter() to get only the keys with true values.

Object.keys($scope.generated.codesWithBalance).filter(function(key, i, array) {
    return array[key];
}).length;

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