set.flatMap()
Description
The flatMap()
method creates an Set by invoking function on
each element of the calling set and flattening the
resulting set one level. The Set elements are passed as a
parameter to function, sequentially.
The calling set isn’t changed.
Iterator methods
FQL provides several methods for iterating over a set.
set.forEach()
and
set.map()
are similar but used for
different purposes:
Method | Primary use | Notes |
---|---|---|
Perform in-place writes on set elements. |
Doesn’t return a value. |
|
Return a new set with projected or transformed elements. |
Can’t perform writes. |
|
Similar to |
Can’t perform writes. |
Parameters
Return value
Type | Description |
---|---|
Set of the result of calling function on each element of the calling set. The resulting set is flattened by one level. |
Examples
The following query uses map()
and returns a nested set.
// Returns a set
let topCustomer = Customer.byEmail("alice.appleseed@example.com")
// Returns a nested set
topCustomer.map((customer) => Order.byCustomer(customer))
{
data: [
{
data: [
{
id: "<ORDER_DOCUMENT_ID>",
coll: Order,
ts: Time("2099-07-31T12:42:19Z"),
items: "hdW...",
total: 5392,
status: "cart",
customer: Customer("<CUSTOMER_DOCUMENT_ID>"),
createdAt: Time("2099-07-31T12:42:18.774426Z"),
payment: {}
}
]
}
]
}
The following query uses flatmap()
to flatten the returned set.
// Returns a set
let topCustomer = Customer.byEmail("alice.appleseed@example.com")
// Returns a flattened set
topCustomer.flatMap((customer) => Order.byCustomer(customer))
{
data: [
{
id: "<ORDER_DOCUMENT_ID>",
coll: Order,
ts: Time("2099-07-31T12:42:19Z"),
items: "hdW...",
total: 5392,
status: "cart",
customer: Customer("<CUSTOMER_DOCUMENT_ID>"),
createdAt: Time("2099-07-31T12:42:18.774426Z"),
payment: {}
}
]
}
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!