0

I am trying to display exactly how many of an array variable I have, not just the items in the array, but one of the 'tags' assigned to it.

$scope.products = [
    {
      too: 'xxx-xxx-xxx',
       foo:  'xxx',
        bar: 'xxx-xxx',
                }
                }
            ];

When I try to list the length of one of the 'tags' for example "Foo".

Using {{foo.length}} will not work.

I would want my HTML to look something like this:

Menu 1 (1) <--- With this being the "foo" value.

*The foo value will change based on the amount of 'items' i have in my array, so if I had an array like this:

   $scope.products = [
                   {
          too: 'xxx-xxx-xxx',
           foo:  'xxx',
            bar: 'xxx-xxx',
                    }
            {
              too: 'yyy-yyy-yyy',
               foo:  'yyy',
                bar: 'yyy-yyy',
                        }
                        }
                    ];

the HTML would reflect that I now have '2' values of 'foo', so it would say Menu 1 (2), and so on and so forth.. How can I accomplish this?

2
  • $scope.products.filter(function (item) { return item.hasOwnProperty('foo') }).length -- try this. This will give the number of elements having foo property Commented Feb 26, 2015 at 14:05
  • I added that into a new function, should I just print it to screen? or is there a better solution for that? Commented Feb 26, 2015 at 14:09

1 Answer 1

1

You can write a javascript function and just call it. (updated to only count unique foos)

$scope.countUniqueFoos = function () {
    var uniqueFoos = []; //you could also keep this as a $scope variable if needed
    $scope.products.forEach( function (product) {
        if (product.hasOwnProperty('foo') && uniqueFoos.indexOf(product.foo) == -1) {
             uniqueFoos.push(product.foo);
        }
    });
    return uniqueFoos.length;
};

And in the HTML:

<div>NumFoos: {{countUniqueFoos()}}</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, so far this works! My only concern is, is it just counting how many times 'foo' appears in my products array? or is it counting individual foo values, said one value was 'xxx' and another 'yyy'. This function would count (2). If I had a duplicate value such as 'xxx', 'xxx', and 'yyy'. Would it count 2 since 'xxx' and 'xxx' are the same? or would it count 3 since there are 3 instances of 'foo'? Edit: Just did a check, and it counts number 'foo' appears, not the number of unique 'foos' (xxx, yyy, zzz) = 3 but (xxx, xxx, yyy) should only = 2.
That would be wonderful, I appreciate the help. In the mean time I'll keep researching the issue myself. Thanks!
Wow! Thank you for the fast response, this is excellent! Exactly what I needed, thanks!

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.