筛选节点
概述
Filter节点用于根据对应的布尔值数组来过滤数组。该节点接收任意数据类型的数组和一个长度相同的布尔值数组。然后它会过滤该数组,只保留对应布尔值为true
的元素。
当您需要根据某些条件过滤数组时,这个节点特别有用。例如,您可以使用拆分(参见Splitting)Compare Node将数组中的每个元素与某个值进行比较,然后使用Filter Node根据比较结果来过滤数组。
- 输入
- 输出
- 编辑器设置
输入项
标题 | 数据类型 | 描述 | 默认值 | 备注 |
---|---|---|---|---|
Array | any[] | The array to be filtered. | (required) | The input will be coerced into an array if it is not an array. |
Include | boolean[] | An array of boolean values indicating whether to include each element in the array. | (required) | The input will be coerced into a boolean array if it is not a boolean array. The length of this array should match the length of the Array input. |
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
Filtered | any[] | The filtered array, containing only the elements where the corresponding boolean value in the Include input was true . | The output will be an array of the same data type as the Array input. If the Array input is not an array, the output will be an array of any . |
编辑器设置
该节点没有可配置的编辑器设置。
示例1:过滤数字数组
- 创建一个Array Node并将值设置为
[1, 2, 3, 4, 5]
。 - 创建另一个Array Node并将值设置为
[true, false, true, false, true]
。 - 创建一个筛选节点,将第一个数组节点连接到
Array
输入,将第二个数组节点连接到Include
输入。 - 运行图形。Filter节点的
Filtered
输出应为[1, 3, 5]
。
示例2:根据条件过滤数组
- 创建一个Array Node并将值设置为
[1, 2, 3, 4, 5]
。 - 创建一个Compare Node并将比较运算符设置为
>
。在Compare节点上启用拆分功能。 - 创建一个Number Node并将值设为
3
。 - 将Array节点连接到Compare节点的
A
输入,并将Number节点连接到Compare节点的B
输入。 - 创建一个筛选节点,并将比较节点连接到
Array
输入,同时将比较节点的Result
输出连接到Include
输入。 - 运行图形。Filter节点的
Filtered
输出应为[4, 5]
。
错误处理
如果未提供Array
或Include
输入,或者数组长度不匹配,过滤节点将报错。
常见问题
问:如果Array和Include输入的长度不匹配会发生什么?
A: Filter Node 会报错。Array 和 Include 输入的长度应始终保持一致。
问:我可以使用Filter节点来过滤对象数组吗?
答:是的,您可以使用Filter节点来过滤任何数据类型的数组,包括对象。只需确保提供一个布尔值数组,指示是否在过滤后的数组中包含每个对象。