简体   繁体   English

如何从数组中拆分指定数量的元素?

[英]How to split an specified number of elements from an array?

I have an array below: 我下面有一个数组:

var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];

and the length of the array is 20 and i am spliting it with 5 和数组的长度是20我将其拆分为5

When i specify 1, it should return [1,2,3,4,5] and if i specify 2, it should return [6,7,8,9,10] 当我指定1时,它应返回[1,2,3,4,5],如果我指定2,则应返回[6,7,8,9,10]

How can i get it through scripting? 如何通过脚本获取它?

Thanks for the help 谢谢您的帮助

You can use the .slice() method to return a segment from an array. 您可以使用.slice()方法从数组返回段。

MDN documentation MDN文档

For your case, you'd do something like this. 对于您的情况,您将执行以下操作。

arr.slice(5 * n, 5 * n + 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM