types.Array.filter
Collects the elements for which callback returns a true value.
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
callback | function | callback(value, [index], [array]), tested against each element, in order |
Return value
A new array of the elements that passed.
Errors
Whatever callback throws, on the first element where it throws.
Notes
callback gets exactly as many of the arguments listed above as it declares parameters for, and never more.
Example
let a = [1, 2, 3, 4, 5];
let evens = a.filter(function(v) { return v % 2 == 0 });
println("evens =", ", ".join(evens.map(@(v) v.tostring())));
println("evens == a:", evens == a); // filter() always returns a new arrayOutput:
evens = 2, 4
evens == a: falseSee also
map | Builds a new array from the results of callback on each element. |
findvalue | Finds the first element for which predicate returns a true value. |
| array | class index |