0

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;       
};
Axon
  • 251
  • 1
  • 2
  • 9

4 Answers4

2

try to use this code to get object length.

Object.keys(program).length;
Nikdyvice
  • 101
  • 4
0

try : How to display length of filtered ng-repeat data, so you can access size by $scope.filtered.length like the example.

Community
  • 1
  • 1
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
0

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

Viral Shah
  • 47
  • 5
0

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.

JackDev
  • 4,891
  • 1
  • 39
  • 48