1 |
|
2 |
|
3 | # Function filter
|
4 |
|
5 | Filter the items in an array or one dimensional matrix.
|
6 |
|
7 |
|
8 | ## Syntax
|
9 |
|
10 | ```js
|
11 | math.filter(x, test)
|
12 | ```
|
13 |
|
14 | ### Parameters
|
15 |
|
16 | Parameter | Type | Description
|
17 | --------- | ---- | -----------
|
18 | `x` | Matrix | Array | A one dimensional matrix or array to filter
|
19 | `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.
|
20 |
|
21 | ### Returns
|
22 |
|
23 | Type | Description
|
24 | ---- | -----------
|
25 | Matrix | Array | Returns the filtered matrix.
|
26 |
|
27 |
|
28 | ## Examples
|
29 |
|
30 | ```js
|
31 | function isPositive (x) {
|
32 | return x > 0
|
33 | }
|
34 | math.filter([6, -2, -1, 4, 3], isPositive) // returns [6, 4, 3]
|
35 |
|
36 | math.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)
|