Contents

Roo

Array.each_index()

Iterates through this array, calling the passed function for each element, passing to the function the index as the first parameter. The method can take an optional second parameter as an Array object (arguments). The elements of this array will be passed to the function as additional parameters.

array.each_index(func as Function, arguments? as Array) as Array

Parameters

  • func. A reference to a function. This function must have been defined with at least one parameter as the current element’s index will be passed to it. This function may have additional parameters defined and these will be passed to it as the contents of the optional arguments array
  • arguments. Optional Array object. Each element of this array will be passed to func as additional parameters after the current element’s index

Returns

Returns this array, unaltered.

Example

function stars(i) {
    # `i` will be the current element's index, passed as the first parameter.
    print("*" + i + "*")
}

function prefix(i, what) {
    print(what + i)
}

var a = ["a", "b", "c"]
a.each_index(stars)
# Prints:
# *0*
# *1*
# *2*

a.each_index(prefix, ["->"])
# Prints:
# ->0
# ->1
# ->2

print(a) # => [a", "b", "c"]. Note unaltered.