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()
,
set.map()
,
set.flatMap()
are similar but used
for different purposes:
Method | Primary use | Notes |
---|---|---|
Perform in-place writes on Set elements. |
Doesn’t return a value. |
|
Returns a new Set. |
Can’t perform writes. |
|
Similar to |
Can’t perform writes. |
For examples, see:
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
Basic example
// Returns a Set
let topCustomer = Customer.byEmail("alice.appleseed@example.com")
// Returns a flattened Set
topCustomer.flatMap((customer) => Order.byCustomer(customer))
{
data: [
{
id: "410674590662000717",
coll: Order,
ts: Time("2099-10-02T22:37:39.583Z"),
items: "hdW...",
total: 5392,
status: "cart",
customer: Customer("111"),
createdAt: Time("2099-10-02T22:37:39.434810Z"),
payment: {}
}
]
}
set.map()
vs. set.flatMap()
set.flatMap()
is similar to
set.map()
, except
set.flatMap()
also flattens the
resulting Set by one level.
In the following example, set.map()
returns a two-dimensional Set:
// Get a Set of all `Category` collection documents.
let categories = Category.all()
// Use `map()` to get a Set of `Product` documents
// for each category.
categories.map(category => {
let category: Any = category
Product.byCategory(category)
})
// Two-dimensional Set.
{
data: [
{
data: [
{
id: "111",
coll: Product,
ts: Time("2099-10-02T22:37:39.583Z"),
name: "cups",
description: "Translucent 9 Oz, 100 ct",
price: 698,
stock: 100,
category: Category("123")
},
...
]
},
{
data: [
{
id: "333",
coll: Product,
ts: Time("2099-10-02T22:37:39.583Z"),
name: "pizza",
description: "Frozen Cheese",
price: 499,
stock: 100,
category: Category("456")
}
]
},
{
data: [
{
id: "444",
coll: Product,
ts: Time("2099-10-02T22:37:39.583Z"),
name: "avocados",
description: "Conventional Hass, 4ct bag",
price: 399,
stock: 1000,
category: Category("789")
},
...
]
}
]
}
To flatten the result to a one-dimensional array, use
set.flatMap()
instead:
// Get a Set of all `Category` collection documents.
let categories = Category.all()
// Use `flatMap()` to get a Set of `Product` documents
// for each category. Then flatten the resulting Set.
categories.flatMap(category => {
let category: Any = category
Product.byCategory(category)
})
// One-dimensional Set.
{
data: [
{
id: "111",
coll: Product,
ts: Time("2099-10-02T22:37:39.583Z"),
name: "cups",
description: "Translucent 9 Oz, 100 ct",
price: 698,
stock: 100,
category: Category("123")
},
...
{
id: "333",
coll: Product,
ts: Time("2099-10-02T22:37:39.583Z"),
name: "pizza",
description: "Frozen Cheese",
price: 499,
stock: 100,
category: Category("456")
},
...
{
id: "444",
coll: Product,
ts: Time("2099-10-02T22:37:39.583Z"),
name: "avocados",
description: "Conventional Hass, 4ct bag",
price: 399,
stock: 1000,
category: Category("789")
}
]
}
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!