1. Docs
  2. Formulas
  3. Functions

REDUCE

Iterates over elements in an array, applying a specified lambda function to accumulate a result. This function takes 3 arguments `array`: The iterable to be reduced. `LAMBDA`: A lambda function with two arguments (`previous` and `current`) that defines the reduction operation. Optionally, an expression `[expr]` can be included for additional computation. `initial_value`: an initial value for the accumulator. The reduction starts with this value.

Categories: Lambdas

Syntax

REDUCE(array, LAMBDA(previous_value, current_value, [expr]), initial_value)

Examples

Example 1

Input

1
{
2
"my_array": [
3
1,
4
2,
5
3,
6
4,
7
5,
8
6,
9
7,
10
8,
11
9,
12
10
13
]
14
}

Formula

REDUCE(my_array, LAMBDA(previous_value, current_value, PLUS(previous_value, current_value)), 0)

Output

55

Example 2

Input

1
{
2
"array_of_objects": [
3
{
4
"timestamp": "2022-02-15 12:30:00+0000"
5
},
6
{
7
"user": "bob.smith"
8
},
9
{
10
"src_ip": "192.168.1.100"
11
},
12
{
13
"dest_ip": "172.16.1.23"
14
},
15
{
16
"file_hash": "2968896cb4639d4c839ab802720266d35bb15be93e3a4055109aa3bf1779f591"
17
},
18
{
19
"file_name": "executable.exe"
20
}
21
]
22
}

Formula

REDUCE(array_of_objects, LAMBDA(previous_value, current_value, MERGE(previous_value, current_value)), OBJECT())

Output

1
{
2
"dest_ip": "172.16.1.23",
3
"file_hash": "2968896cb4639d4c839ab802720266d35bb15be93e3a4055109aa3bf1779f591",
4
"file_name": "executable.exe",
5
"src_ip": "192.168.1.100",
6
"timestamp": "2022-02-15 12:30:00+0000",
7
"user": "bob.smith"
8
}

Sample actions

Event Transform
REDUCE
Event Transform
My Action

Select an action to inspect

You can also click "Copy actions" and paste them in your Tines story to see how they work.

Was this helpful?