fold()

Call the reducer for every element of an array, using a seed as the first acc (accumulator).

Signature

fold(seed: U, reducer: (acc: U, value: T) => U): U

Description

The fold() method calls reducer for every element of the array, using a seed as the first acc (accumulator).

The source array isn’t changed.

Parameters

Parameter Type Required Description

seed

Any

Yes

Initial state value provided to the reducer Function.

reducer

Function

Yes

Anonymous Function to invoke for each Array value.

reducer parameters:

Parameter Type Required Description

acc

Any

Yes

Value returned by the previous invocation of reducer. On the first invocation of reducer, seed is passed as the acc (accumulator).

value

Any

Yes

Current Array value.

Return value

Type Description

Any

Result of the last reducer invocation. The seed is returned for an empty array.

Examples

let iter = [1, 2, 3]
iter.fold(100, (value, elem) => value + elem)
106

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!