如何从数组中获取子数组?

我有var ar = [1, 2, 3, 4, 5]并想要一些函数getSubarray(array, fromIndex,

toIndex),调用的结果getSubarray(ar, 1, 3)是新数组[2, 3, 4]

回答:

看一眼 Array.slice(begin,

end)

const ar  = [1, 2, 3, 4, 5];

// slice from 1..3 - add 1 as the end index is not included

const ar2 = ar.slice(1, 3 + 1);

console.log(ar2);

以上是 如何从数组中获取子数组? 的全部内容, 来源链接: utcz.com/qa/417933.html

回到顶部