UNPKG

1.12 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function filter
4
5Filter the items in an array or one dimensional matrix.
6
7
8## Syntax
9
10```js
11math.filter(x, test)
12```
13
14### Parameters
15
16Parameter | Type | Description
17--------- | ---- | -----------
18`x` | Matrix &#124; Array | A one dimensional matrix or array to filter
19`test` | Function &#124; 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.
20
21### Returns
22
23Type | Description
24---- | -----------
25Matrix &#124; Array | Returns the filtered matrix.
26
27
28## Examples
29
30```js
31function isPositive (x) {
32 return x > 0
33}
34math.filter([6, -2, -1, 4, 3], isPositive) // returns [6, 4, 3]
35
36math.filter(["23", "foo", "100", "55", "bar"], /[0-9]+/) // returns ["23", "100", "55"]
37```
38
39
40## See also
41
42[forEach](forEach.md),
43[map](map.md),
44[sort](sort.md)