tommyd456
tommyd456

Reputation: 10683

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?

Upvotes: 2

Views: 4397

Answers (1)

Alexander Kravets
Alexander Kravets

Reputation: 4395

Use this:

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

Upvotes: 6

Related Questions