Function filter #

Filter the items in an array or one dimensional matrix.

The callback is invoked with three arguments: the current value, the current index, and the matrix operated upon. Note that because the matrix/array might be multidimensional, the “index” argument is always an array of numbers giving the index in each dimension. This is true even for vectors: the “index” argument is an array of length 1, rather than simply a number.

Syntax #

math.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.

Throws #

Type | Description —- | ———–

Examples #

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

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

See also #

forEach, map, sort

Fork me on GitHub