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;       
};
4
  • 1) case is a reserved keyword, you cannot use it as a variable name. 2) how are you calling the filter? Commented Jun 9, 2016 at 13:03
  • "Case" is just a placeholder. Filter is working as it should. I just need json object length. Commented Jun 9, 2016 at 13:05
  • is program a json? Commented Jun 9, 2016 at 13:12
  • Yes it is. I got length in http.get and then passed it to filter, it is the only way i got it to work. Commented Jun 9, 2016 at 13:14

4 Answers 4

2

try to use this code to get object length.

Object.keys(program).length;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.