Skip to content

distinctBy

distinctBy(array, keyFn) → array · Array

Remove duplicate values from an array using a key extractor to determine uniqueness. Keeps the first element for each key.

  • array (required): the array to deduplicate

  • keyFn (required): lambda (element) -> key

bash
echo '{"orders": [{"id": 1, "cust": "C-42"}, {"id": 2, "cust": "C-42"}, {"id": 3, "cust": "C-41"}]}' \
  | utlx -e 'distinctBy($input.orders, (o) -> o.cust)'
# [{"id": 1, "cust": "C-42"}, {"id": 3, "cust": "C-41"}]
utlx
{
  onePerCustomer: distinctBy($input.orders, (o) -> o.customerId)
}
// keeps first order per customer, removes duplicates

Released under AGPL-3.0.