Quirrel4.41.0

types.Array.filter

Method on every array
array.filter(callback: function): any

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

callbackfunctioncallback(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

examples/types/array/filter.nut
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 array
Output:
evens = 2, 4
evens == a: false

See also

mapBuilds a new array from the results of callback on each element.
findvalueFinds the first element for which predicate returns a true value.
arrayclass index