简体   繁体   English

AngularJS-在自定义过滤器中获取对象索引

[英]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. 我在控制器中做了一个自定义过滤器,但无法获取迭代json的索引或大小。 Calling program.length gets me undefined. 调用program.length使我不确定。 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 []. length()方法仅在数组[]上可用。

In this case you are trying to get the size of a json object {}, please note this does not have a length. 在这种情况下,您尝试获取json对象{}的大小,请注意,它没有长度。 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. 如果需要查看此对象中存在多少项,可以尝试首先使用Object.keys(program)将所有键转换为数组,这将为您提供一个数组。

If program originally looked like this: { foo: 'some value', bar: 'another value' } 如果program最初看起来像这样:{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. 使用Object.keys(program)将给您['foo', 'bar']在此数组上,您现在可以调用length方法,这样您就有Object.keys(program).length ,在这种情况下,它将给您2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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