简体   繁体   中英

AngularJS - Get object index in custom filter

I have made a custom filter in controller but can't get index or size of iterated json. Calling program.length gets me undefined. Any ideas?

$scope.filterFn = function(program){
    if(case){
        return true;
    }
    console.log(program.length);//undefined
    return false;       
};

尝试使用此代码获取对象长度。

Object.keys(program).length;

try: 如何显示过滤后的ng-repeat数据的长度 ,因此可以像示例一样通过$scope.filtered.length访问size。

Are there any more filters before this? Coz previous filters decide input for next filter?

The length() method is only available on an array [].

In this case you are trying to get the size of a json object {}, please note this does not have a length. If you need to see how many items exist in this object, you can try to first convert all the keys into an array by using Object.keys(program) which will give you an array.

If program originally looked like this: { foo: 'some value', bar: 'another value' }

using Object.keys(program) will give you ['foo', 'bar'] On this array you can now call the length method, so that you have Object.keys(program).length , which in this case would give you 2.

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