Quirrel4.41.0

types.Array.each

Method on every array
array.each(callback: function): any

Calls callback once per element, in order.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

callbackfunctioncallback(value, [index], [array]), called for each element

Return value

Nothing (null); each is for the side effect, not for a result.

Errors

Whatever callback throws, on the first element where it throws; iteration stops there.

Notes

callback gets exactly as many of the arguments listed above as it declares parameters for, and never more.

Example

examples/types/array/each.nut
let a = [10, 20, 30];
local total = 0;
let result = a.each(function(v, i) { total += v * i });
println("total =", total);      // 10*0 + 20*1 + 30*2
println("result =", result);      // each() has no return value of its own
Output:
total = 80
result = null

See also

mapBuilds a new array from the results of callback on each element.
reduceFolds the array down to one value by repeatedly combining an accumulator with each element.
arrayclass index