Contents

Roo

Array.reject()

Iterates through this array, calling the passed function for each element, passing to the function an element 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. If the passed function returns True then that element will be discarded, otherwise the element will be kept in the new array. The new array is then returned and this array is left unaltered. It’s the opposite of the Array.keep() method.

array.reject(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 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. If this function returns True then this element will be discarded from the new array, otherwise the element will be kept
  • arguments. Optional Array object. Each element of this array will be passed to func as additional parameters after the current element

Returns

  • A new array containing the elements for which func returned False

Example

function too_big?(i) {
    # Reject any elements bigger than 5.
    return i > 5 ? True : False 
}

var a = [2, 4, 6, 8, 10]
var b = a.reject(too_big?)
print(a) # => [2, 4, 6, 8, 10]
print(b) # => [2, 4]