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]]
网友评论