array.forEach()

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.

Invoke a Function for each Array element.

Signature

forEach(function: (val: Any) => Any): Null

Description

The forEach() method sequentially calls function for each Array element, passing the element value as an argument to function.

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 array.map(), but flattens the resulting array by one level.

Can’t perform writes.

Parameters

Parameter Type Required Description

function

Function

Yes

Function to invoke for each Array element in the calling Array. Each Array element is passed to function as an argument.

function parameters:

Parameter Type Required Description

val

Any

Current array element.

Return value

Type Description

Null

The return value of function is discarded.

Examples

Multiply each Array element by two:

[1, 2, 3, 4, 5].forEach((x) => {x * 2})
null

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!