切片节点
概述
Slice节点用于提取数组的一部分。它接收一个数组作为输入,并输出一个新的数组,该数组从指定的索引开始并包含指定数量的元素。
该节点对于操作数组非常有用,例如从更大的数据集中提取子集数据。
- 输入
- 输出
- 编辑器设置
输入项
标题 | 数据类型 | 描述 | 默认值 | 备注 |
---|---|---|---|---|
Input | any[] | The array to be sliced. | (required) | The input will be coerced into an array if it is not an array. |
Start | number | The index at which to start the slice. | 0 | The input will be coerced into a number if it is not a number. |
Count | number | The number of elements to include in the slice. If not provided, all elements will be included. | (all) | The input will be coerced into a number if it is not a number. This input is optional. |
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
Output | any[] | The sliced array. | The output will be an array containing the sliced elements from the input array. |
编辑器设置
设置 | 描述 | 默认值 | 使用输入切换 | 输入数据类型 |
---|---|---|---|---|
Start | The index at which to start the slice. If not provided, the slice will start at the beginning. | 0 | Yes | number |
Count | The number of elements to include in the slice. If not provided, all elements will be included. | (all) | Yes | number |
示例1:切片数组
- 创建一个Array Node并将值设置为
[1, 2, 3, 4, 5]
。 - 创建一个 Slice 节点,将
Start
设置为2
,将Count
设置为2
。 - 将Array节点连接到Slice节点的
Input
。 - 运行图表。Slice节点的
Output
输出应为[3, 4]
。
错误处理
如果未提供Input
或其不是数组,Slice节点将报错。如果Start
或Count
不是数字,也会报错。
常见问题
问:如果Start
大于数组长度会发生什么?
A: Slice节点将返回一个空数组。
问:如果Count
大于数组中的元素数量会发生什么?
A: Slice节点将返回从Start
到数组末尾的所有元素。
问:我可以在Start
和Count
中使用负数吗?
A: 是的,您可以使用负数作为Start
参数来从数组末尾开始计数。但是,如果对Count
参数使用负数将会导致错误。