ArrayReduce

Run the provided udf over the array to reduce the values to a single output

Method Signature

ArrayReduce(array=[array], callback=[function:BiFunction], initialValue=[any])

Arguments

Argument
Type
Required
Description
Default

array

array

true

The array to reduce

callback

function:BiFunction

true

The function to invoke for each item. The function will be passed 3 arguments: the accumulator, the current item, and the current index. You can alternatively pass a Java BiFunction which will only receive the first 2 args. The function should return the new accumulator value.

initialValue

any

false

The initial value of the accumulator

Examples

Simple arrayReduce Example

Sum each a element in the array

Run Example

complexData = [ 
	{
		A : 4
	},
	{
		A : 18
	},
	{
		A : 51
	}
];
sum = arrayReduce( complexData, ( Any prev, Any element ) => {
	return prev + element.A;
}, 0 );
writeOutput( sum );

Result: 73

Additional Examples

Run Example

reduced = ArrayReduce( [ 
	1,
	2,
	3,
	4
], ( Any carry, Any value ) => {
	return carry + value;
}, 0 );
dump( reduced ); // yields 10
reduced = ArrayReduce( [
	"hello",
	"there",
	"boxlang"
], ( Any carry, Any value ) => {
	return carry & " " & value;
}, "" );
dump( reduced );
 // yields 'hello there boxlang'

Last updated

Was this helpful?