set.firstWhere()

Get the first matching value from the Set.

Signature

firstWhere(predicate: () => Boolean | Null) => T | Null

firstWhere(predicate: (value: T) => Boolean | Null) => T | Null

Description

The firstWhere() method gets the first matching value from the Set.

Parameters

Parameter Type Required Description

predicate

Function

Yes

Anonymous Function that compares a Set value and returns true for matches, false for mismatches, or Null.

predicate parameters:

Parameter Type Required Description

value

Any

Set value to compare. If omitted, use dot notation to access value properties.

Return value

Type Description

Any

First matching value in the Set or null if the Set is empty or no values match.

Examples

Get the first Customers document where the state property is DC:

Customer.all().firstWhere(.address.state == 'DC')
{
  id: "111",
  coll: Customer,
  ts: Time("2099-07-31T12:42:19Z"),
  cart: Order("412571379960906240"),
  orders: "hdW...",
  name: "Alice Appleseed",
  email: "alice.appleseed@example.com",
  address: {
    street: "87856 Mendota Court",
    city: "Washington",
    state: "DC",
    postalCode: "20220",
    country: "US"
  }
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!