我有var ar = [1, 2, 3, 4, 5]并且想要一些函数getSubarray(array, fromIndex, toIndex) ,调用getSubarray(ar, 1, 3)是新数组[2, 3, 4] 。
var ar = [1, 2, 3, 4, 5]
getSubarray(array, fromIndex, toIndex)
getSubarray(ar, 1, 3)
[2, 3, 4]
Take a look at Array.slice(begin, end)(看一下Array.slice(begin, end))
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);
1.4m articles
1.4m replys
5 comments
57.0k users