array.filter() 函数
array.filter() 是实验性的,并且 随时可能发生变化。
array.filter() 遍历一个数组,使用谓词函数评估每个元素,然后返回一个仅包含符合谓词的元素的新数组。
已弃用
实验性 array.filter() 已被弃用,支持
array.filter()。
函数类型签名
(<-arr: [A], fn: (x: A) => bool) => [A]
有关更多信息,请参见 Function type signatures。
参数
数组
要过滤的数组。默认为管道转发的数组 (<-)。
函数
(必需)
对每个元素评估的谓词函数。
元素在谓词函数中由 x 表示。
示例
过滤整数数组
import "experimental/array"
a = [
1,
2,
3,
4,
5,
]
b = a |> array.filter(fn: (x) => x >= 3)
// b returns [3, 4, 5]
// Output the filtered array as a table
array.from(rows: b |> array.map(fn: (x) => ({_value: x})))