javascript怎么把一个元素列表中的以4个元素为一个单位,
2018-05-23 本文已影响0人
sponing
Array.prototype.chunk = function (len) {
var len = parseInt(len);
if (len <= 1 || this.length < len)
return this;
var groups = [], loop = Math.ceil(this.length / len);
for (var i = 0; i < loop; i++)
groups.push(this.slice(len * i, len * (i + 1)));
return groups;
}
var arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
arr=arr.chunk(4); // [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]