Skip to main content
Version: Next

Reduce

Executes a function on each item of a list to create a unique "reduced" value from it.

Description

The reduce brick executes the user-provided function (reducer) on each item of the provided list, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements or the list is the single result output value of the brick.

In other words, Reduce derives a single value from multiple elements of an array and is equivalent to Javascript's <a href="https://medium.com/@JeffLombardJr/understanding-foreach-map-filter-and-find-in-javascript-f91da93b9f2c" target="_blank">reduce()</a>

The output control flow is triggered when all items have been processed.

An initial value can be provided to process the first element of the list.

Example:

  • inputs
    1. list = [1, 2, 3]
    2. reducer = (sum, item) =&gt; sum + item
    3. initial value = 0
  • outputs
    1. result = 6

Inputs

  • Control Flow (Control Flow)
  • list (List): The original list to iterate on.
  • reducer (Reducer): The function to execute for each item of the list and returns the next resulting value.
    Inputs
    - start (Control Flow): Triggered when the item is to be processed.
    - accumulator (Object): The object accumulating the values across all list items.
    - item (Object): The current item of the list.
    - rank (Number): The rank of the current item.
    - list (List): The full list being iterated.

    Outputs:
    - end (Control Flow): Triggered when the processing is completed.
    - result (Object): The object accumulating the values. Once all list items have been processed, it becomes Reduce's result.
  • initial value (Object): The initial resulting value

Outputs

  • Control Flow (Control Flow)
  • result (Object): The return value after calling the reducer on all items of the list.