array_all

This function is used to check if all of the elements in the given array match the same condition. You check that by passing a Predicate Method that runs on each element of the given array, and returns true or false.

This function returns true if your predicate function returns true for all of the elements in the given array range, otherwise it returns false.

Predicate FunctionPredicate Function

This function takes a Predicate Method that operates on the given array. The predicate function is passed the following arguments:

Syntax:

function(element, index);

ArgumentTypeDescription
elementAnyThe current array element's value
indexRealThe current array index

The predicate function should return a Boolean, which affects how the original function modifies or reads the array.

See information and examples on Predicate Method.


Syntax:

array_all(array, function, [offset], [length]);

ArgumentTypeDescription
arrayArrayThe array to use
functionFunctionThe Predicate Method to run on each element
offsetRealOPTIONAL The offset, or starting index, in the array. Setting a negative value will count from the end of the array. The starting index will then be array_length(array) + offset. See: Offset And Length
lengthRealOPTIONAL The number of elements to traverse. A negative value will traverse the array backwards (i.e. in descending order of indices, e.g. 2, 1, 0 instead of 2, 3, 4). See: Offset And Length

Returns:

Boolean (whether the function returned true for all elements in the array or range)

 

Example:

function is_even(element, index)
{
    return (element mod 2 == 0);
}
values = [2, 4, 8, 10, 12, 14, 18, 22, 46];
var all_elements_are_even = array_all(values, is_even);

The above code first defines a function is_even that returns true if the value is even.

It then creates an array values and adds some numbers to it.

Finally it calls array_all on the array and stores the result in a temporary variable all_elements_are_even. As all values in the array are even, all_elements_are_even will be set to true.