Foreach
Foreach( array_or_page, lambda )
Description
The Foreach
function applies the
Lambda serially to each member of an
Array or Page, and
returns the original, unmodified array.
The Foreach
function is very useful when the original array does not
need to be modified, but a side effect is required for every value in an
array. Later invocations of the Lambda can see the side effects of
earlier invocations of the Lambda.
Examples
The query below iterates over the results returned by the Paginate function, executing the Lambda for each value in the page of results. The page of results contains an array of references, and each reference’s document is updated by the Lambda.
client.query(
q.Foreach(
q.Paginate(
q.Match(q.Index('spells_by_element'), 'fire')
),
q.Lambda(
'spell',
q.Update(
q.Var('spell'),
{
data: {
spellbook: q.Ref(
q.Collection('spellbooks'),
'181388642139243008',
),
},
},
),
),
)
)
.then((ret) => console.log(ret))
{ data:
[ Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))),
Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
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!