Function filter

Filter the items in an array or one dimensional matrix.

Syntax

filter(x, test)

Parameters

Parameter Type Description
x Matrix | Array A one dimensional matrix or array to filter
test Function | RegExp A function or regular expression to test items. All entries for which test returns true are returned. When test is a function, it is invoked with three parameters: the value of the element, the index of the element, and the matrix/array being traversed. The function must return a boolean.

Returns

Type Description
Matrix | Array Returns the filtered matrix.

Examples

function isPositive (x) {
  return x > 0;
}
filter([6, -2, -1, 4, 3], isPositive); // returns [6, 4, 3]

filter(["23", "foo", "100", "55", "bar"], /[0-9]+/); // returns ["23", "100", "55"]

See also

sort