Skip to content

countBy

countBy(array, predicate) → number · Array

Count elements in an array that match a predicate.

  • array (required): the array to count

  • predicate (required): lambda (element) -> boolean

bash
echo '{"orders": [{"status": "ACTIVE"}, {"status": "CLOSED"}, {"status": "ACTIVE"}]}' \
  | utlx -e 'countBy($input.orders, (o) -> o.status == "ACTIVE")'
# 2
utlx
let active = filter($input.orders, (o) -> o.status == "ACTIVE")
{
  activeCount: count(active),
  closedCount: countBy($input.orders, (o) -> o.status == "CLOSED"),
  activeNames: map(active, (o) -> o.name)
}

Released under AGPL-3.0.