开发者社区> 问答> 正文

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

我有var ar = [1, 2, 3, 4, 5]并想要一些函数getSubarray(array, fromIndex, toIndex),调用的结果getSubarray(ar, 1, 3)是新数组[2, 3, 4]。

展开
收起
保持可爱mmm 2020-01-16 15:31:26 545 0
1 条回答
写回答
取消 提交回答
  • 看一眼 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); 问题来源于stack overflow

    2020-01-16 15:31:41
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载