StructReduce

Run the provided udf against struct to reduce the values to a single output

Method Signature

StructReduce(struct=[structloose], callback=[function], initialValue=[any])

Arguments

Argument
Type
Required
Description
Default

struct

struct

true

The struct to reduce

callback

function

true

The function to invoke for each entry in the struct. The function will be passed 4 arguments: the accumulator, they entry key, the current index, and the original struct. The function should return the new accumulator value.

initialValue

any

false

The initial value of the accumulator

Examples

Script Syntax

Run Example

rainbow = { 
	"Red" : "Whero",
	"Orange" : "Karaka",
	"Yellow" : "Kowhai",
	"Green" : "Kakariki"
};
ui = structReduce( rainbow, ( Any previousValue, Any key, Any value ) => {
	return previousValue & "<dt>#key#</dt><dd>#value#</dd>";
}, "<dl>" ) & "</dl>";
writeDump( rainbow );
writeOutput( ui );

Using Member Function

Run Example

rainbow = { 
	"Red" : "Whero",
	"Orange" : "Karaka",
	"Yellow" : "Kowhai",
	"Green" : "Kakariki"
};
ui = rainbow.reduce( ( Any previousValue, Any key, Any value ) => {
	return previousValue & "<dt>#key#</dt><dd>#value#</dd>";
}, "<dl>" ) & "</dl>";
writeDump( rainbow );
writeOutput( ui );

Additional Examples

Run Example

animals = { 
	COW : {
		NOISE : "moo",
		SIZE : "large"
	},
	PIG : {
		NOISE : "oink",
		SIZE : "medium"
	},
	CAT : {
		NOISE : "meow",
		SIZE : "small"
	}
};
dump( label="All animals", var=animals );
animalInfo = StructReduce( animals, ( Any result, Any key, Any value ) => {
	return arguments.RESULT & "<li>" & arguments.KEY & "<ul><li>Noise: " & arguments.VALUE.NOISE & "</li><li>Size: " & arguments.VALUE.SIZE & "</li></ul></li>";
}, "<ul>" ) & "</ul>";
// Show result
echo( animalInfo );

Last updated

Was this helpful?