拆分阵列成块 [英] Split array into chunks

查看:234
本文介绍了拆分阵列成块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有一个JavaScript数组寻找如下:

Let's say that I have an Javascript array looking as following:

["Element 1","Element 2","Element 3",...]; // with close to a hundred elements.

什么办法将是适当的块(分割)数组与许多较小的阵列,可以说,在10种元素最?

What approach would be appropriate to chunk (split) the array into many smaller arrays with, lets say, 10 elements at its most?

推荐答案

较新的<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/slice\">array.slice方法可以从一开始,中间,或者您需要任何目的数组的结束提取切片。

The newer array.slice method can extract a slice from the beginning, middle, or end of an array for whatever purposes you require.

var i,j,temparray,chunk = 10;
for (i=0,j=array.length; i<j; i+=chunk) {
    temparray = array.slice(i,i+chunk);
    // do whatever
}

这篇关于拆分阵列成块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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