根据javascript中的索引将数组拆分为两个 [英] split an array into two based on a index in javascript

查看:128
本文介绍了根据javascript中的索引将数组拆分为两个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象列表的数组。我想将这个数组拆分为一个特定的索引,比如说4(实际上这是一个变量)。我想将split数组的第二部分存储到另一个数组中。可能很简单,但我无法想到一个很好的方法。

I have an array with a list of objects. I want to split this array at one particular index, say 4 (this in real is a variable). I want to store the second part of the split array into another array. Might be simple, but I am unable to think of a nice way to do this.

推荐答案

使用切片,如下:

var ar = [1,2,3,4,5,6];

var p1 = ar.slice(0,4);
var p2 = ar.slice(4);

这篇关于根据javascript中的索引将数组拆分为两个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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