0

Friends, We are having an array categories of scope variable. Need to get count of checked(checked: true) categories without using for loop. Is there any way? Please suggest. Thanx.

 $scope.categories = [
                       {categoryId : '1', checked: true}, 
                       {categoryId : '2', checked: true},         
                       {categoryId : '3', checked: false}, 
                       {categoryId : '4', checked: false}
                    ];
3
  • I think you have you { and [ mixed up. Commented Apr 23, 2015 at 12:16
  • Why don't you want a for loop? Anything you use will be using one in the background. Commented Apr 23, 2015 at 12:17
  • Can you give how you want to use this count or some more information what is your need and why you don't want to use loop. Personally I don't think there is way to avoid loop. Commented Apr 23, 2015 at 12:20

4 Answers 4

4

You say you don't want to loop, however don't explain why? I don't think you can avoid a loop if you have an unknown length of array. In your case, a filter may do the trick.

Update for clarity
This will use a loop in the background.

var count = $scope.categories.filter(function(cat) { 
    return cat.checked === true 
}).length;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanx for your suggestion simon..Just I removed true var count = $scope.categories.filter(function(cat) { return cat.checked }).length;
No problem :) - The === true part only serves to ensure the checked property is true and not some other truthy value.
Yeah, it really is looping still, you just don't see that it is. Filter means loop over this array, apply the function to each element and return the element if the function returns true.
I know, but I suspect the OP just didn't want to use a for loop for some reason. Even though filter may in the background.
I assumed that you knew based on your answer.. just making sure the OP does.
2

Assuming your data is:

 $scope.categories = [
                      {categoryId : '1', checked: true}, 
                      {categoryId : '2', checked: true},         
                      {categoryId : '3', checked: false}, 
                      {categoryId : '4', checked: false}
                     ];

var total = $scope.categories.reduce(function(t,item){
                return t + +item.checked;
            },0)

1 Comment

This and simonlchilds answers are both equally valid, but this answer is using the right tool for what you're asking.
1

If you really want to avoid using any kind of loop you have to know the amount of items in your array in advance.

Assuming your example is fixed data, then you can simply use this

var count = $scope.categories[0].checked + $scope.categories[1].checked + $scope.categories[2].checked + $scope.categories[3].checked

to get the count. Any other solution will use a loop of some sort.

In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

4 Comments

Well, without using ANY loop you probably have to assume a fixed amount of items in an array... else you will always end up using some kind of loop ;)
You are correct but I am wrong that mentioned small array of elements. If it is big, this is very difficult todo.
And that is where loops come in handy ;)
+1 for the clarity. I like this solution, despite the fact that this would be a fairly poor design decision.
0

Using underscore/lodash:

var count = _.filter($scope.categories, {checked: true}).length;

1 Comment

Your suggestion also correct. Working fine like Simon suggestion. Thanx Tim

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.