array.map()
This method operates on an array. You typically fetch documents from a collection as a set, not an array. For the equivalent set method, see set instance methods. For differences between sets and arrays, see Sets vs. arrays. |
Description
The map()
method creates an Array by sequentially invoking a
function on the elements of the calling array. The new
Array elements are the value of function after each call. The
function parameter can be an anonymous Function or a top-level
object method.
The calling array isn’t changed.
Array iteration methods
FQL provides several methods for iterating over an array.
array.forEach()
and
array.map()
are similar but used
for different purposes:
Method | Primary use | Notes |
---|---|---|
Perform in-place writes on array elements. |
Doesn’t return a value. |
|
Return a new array with projected or transformed elements. |
Can’t perform writes. |
|
Similar to |
Can’t perform writes. |
Parameters
Return value
Type | Description |
---|---|
Array with the result of function invoked on each element of the calling array. |
Examples
-
Create an Array by mapping Array elements to subArrays that are constructed from the element value and the element value plus one, using an anonymous Function:
[1, 2, 3].map(x => [x, x + 1])
[ [ 1, 2 ], [ 2, 3 ], [ 3, 4 ] ]
-
Create an Array by passing a top-level object method as the mapping Function:
[{name: "D"}, {name: "E"}].map(Collection.create)
[ { name: "D", coll: Collection, ts: Time("2099-02-18T20:06:25.620Z"), indexes: {}, constraints: [] }, { name: "E", coll: Collection, ts: Time("2099-02-18T20:06:25.620Z"), indexes: {}, constraints: [] } ]
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!