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:
function(element, index);
Argument | Type | Description |
---|---|---|
element | Any | The current array element's value |
index | Real | The 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.
array_all(array, function, [offset], [length]);
Argument | Type | Description |
---|---|---|
array | Array | The array to use |
function | Function | The Predicate Method to run on each element |
offset | Real | OPTIONAL 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 |
length | Real | OPTIONAL 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 |
Boolean (whether the function returned true for all elements in the array or range)
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.