forEach()

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.

Iterator methods

FQL provides several methods for iterating over an array. forEach() and map() are similar but used for different purposes.

To perform writes on an array’s elements, use forEach(). forEach() doesn’t return a value.

To return a new array containing projected or transformed elements, use map(). map() can’t perform writes.

flatMap() works like map() except it flattens the resulting array by one level.

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!