tommyd456 tommyd456 - 5 months ago 22
AngularJS Question

Using filter to count array length with condition

I have an array of objects called

places
where each object looks something like this:

{
"name" : "London",
"category" : "city"
}


I just use
<span>{{places.length}}</span>
for the total array length but now I would like to count the number of objects that have a category of
city
ideally using a filter directly within the view.

Something like:

{{places.length | filter: ...}}


but not sure how to write the filter?

Answer

Use this:

<span>{{ (places | filter : { category: 'city' }).length }}</span>
Comments