REDUCE

Reduce an array to a single value

Description

Reduces an array to a single value by evaluating a formula for each item in the array, accumulating to a result value which is then passed to the next evaluation.

The iteration is done from the first item to the last item. The iteration starts by using the 1st item in the array as an initial accumulator value. Alternatively you can use REDUCE\_INIT function instead and provide an initial value.

If the array has only one item, that item is returned. If the array is empty, returns null.

Function signature

REDUCE(array: Array, reducerformula: Function) => Object

Parameter

Description

Array

Array of items to reduce

Reducer formula

Formula that returns a reduced result using the current item and the previous accumulated result value

Examples

products = [
{
"name": "Swiss army knife",
"price": 60
},
{
"name": "Duct tape",
"price": 5
},
{
"name": "Glue",
"price": 6
}
]

Formula

Return value

REDUCE(products, IF(item.price < accumulator.price, item, accumulator))

{"name":"Duct tape","price":5}

REDUCE<cheapest, product>(products, IF(product.price < cheapest.price, product, cheapest))

{"name":"Duct tape","price":5}