如何从数组中获取子数组? [英] How to get subarray from array?

查看:33
本文介绍了如何从数组中获取子数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have var ar = [1, 2, 3, 4, 5] and want some function getSubarray(array, fromIndex, toIndex), that result of call getSubarray(ar, 1, 3) is new array [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);

这篇关于如何从数组中获取子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆