Skip to content

at

从数组中获取指定索引处的元素并返回一个新数组。

typescript
const result = at(arr, indices);

参考

at(arr, indices)

当您想从数组中选择特定位置的元素时,请使用 at。您可以使用负索引从数组末尾选择元素。

typescript
import { at } from 'es-toolkit/array';

// 从数字数组中获取多个索引处的元素。
at([10, 20, 30, 40, 50], [1, 3, 4]);
// 返回: [20, 40, 50]

// 使用负索引从末尾获取元素。
at(['a', 'b', 'c', 'd'], [0, -1, -2]);
// 返回: ['a', 'd', 'c']

非整数索引会被转换为整数。

typescript
import { at } from 'es-toolkit/array';

at([1, 2, 3, 4], [1.5, 2.9]); // [2, 3]

参数

  • arr (T[]): 要获取元素的数组。
  • indices (number[]): 要获取的元素的索引数组。负值从数组末尾开始计算。

返回值

(T[]): 包含指定索引处元素的新数组。

采用 MIT 许可证发布。