StructReduce

Iterates over every entry of the given struct and calls the closure with every key/value. This function will reduce the struct to a single value and will return this value.

StructReduce( struct, closure [, initalValue ] )

Returns: any

Argument Description
struct
struct, required

struct to iterate

closure
function, required

function/closure that implements the following constructor [function(any result, string key, object value[, struct struct]):any].

initalValue
object, optional

inital value passed as part of the first closure call

Examples

Member Function

animals = {
	cow: {
		noise: "moo",
		size: "large"
	},
	pig: {
		noise: "oink",
		size: "medium"
	},
	cat: {
		noise: "meow",
		size: "small"
	}
};
// Show all animals
Dump(
	label: "All animals",
	var: animals
);
// call animals.reduce()
animalInfo = animals.reduce(function(result, key, 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);

Non-Member Function

animals = {
	cow: {
		noise: "moo",
		size: "large"
	},
	pig: {
		noise: "oink",
		size: "medium"
	},
	cat: {
		noise: "meow",
		size: "small"
	}
};
// Show all animals
Dump(
	label: "All animals",
	var: animals
);
// call StructReduce(animals)
animalInfo = StructReduce(animals, function(result, key, 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);

See also