## 如何将数组分割成块？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (10)
• 关注 (0)
• 查看 (69)

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

### 10 个回答

array.slice方法可以从数组的开头、中间或结尾提取一个片段来满足你的任何需求，而无需更改原始数组。

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

```function chunk(chunkSize, array) {
return array.reduce(function(previous, current) {
var chunk;
if (previous.length === 0 ||
previous[previous.length -1].length === chunkSize) {
chunk = [];   // 1
previous.push(chunk);   // 2
}
else {
chunk = previous[previous.length -1];   // 3
}
chunk.push(current);   // 4
return previous;   // 5
}, []);   // 6
}

console.log(chunk(2, ['a', 'b', 'c', 'd', 'e']));
// prints [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e' ] ]```

1. 如果先前的值（即之前返回的块的数组）为空或者如果最后一个块有`chunkSize`项，则创建一个新的块
2. 将新块添加到现有块数组中。
3. 否则，当前块是块数组中的最后一个块。
4. 将当前值添加到块中
5. 返回修改过的块数组。
6. 通过传递一个空数组来初始化

```var chunk3 = function(array) {
return chunk(3, array);
};

console.log(chunk3(['a', 'b', 'c', 'd', 'e']));
// prints [ [ 'a', 'b', 'c' ], [ 'd', 'e' ] ]```

```Object.defineProperty(Array.prototype, 'chunk', {
value: function(chunkSize) {
return this.reduce(function(previous, current) {
var chunk;
if (previous.length === 0 ||
previous[previous.length -1].length === chunkSize) {
chunk = [];
previous.push(chunk);
}
else {
chunk = previous[previous.length -1];
}
chunk.push(current);
return previous;
}, []);
}
});

console.log(['a', 'b', 'c', 'd', 'e'].chunk(4));
// prints [ [ 'a', 'b', 'c' 'd' ], [ 'e' ] ]```

```function chunk(arr, n) {
return arr.slice(0,(arr.length+n-1)/n|0).
map(function(c,i) { return arr.slice(n*i,n*i+n); });
}```

`chunk([1,2,3,4,5,6,7], 2);`

```function chunker(p, c, i) {
(p[i/this|0] = p[i/this|0] || []).push(c);
return p;
}```

`[1,2,3,4,5,6,7].reduce(chunker.bind(3),[]);`

```// Fluent alternative API without prototype hacks.
function chunker(n) {
return function(p, c, i) {
(p[i/n|0] = p[i/n|0] || []).push(c);
return p;
};
}```

`[1,2,3,4,5,6,7].reduce(chunker(3),[]);`

```function chunk(arr, n) {
return arr.reduce(function(p, cur, i) {
(p[i/n|0] = p[i/n|0] || []).push(cur);
return p;
},[]);
}

chunk([1,2,3,4,5,6,7], 3);```

```function chunk(arr, chunkSize) {
var R = [];
for (var i=0,len=arr.length; i<len; i+=chunkSize)
R.push(arr.slice(i,i+chunkSize));
return R;
}```

ES6版本:

```X = 2; // items per chunk

a = ['a','b','c','d'];

a.reduce((ar, it, i) => {
const ix = Math.floor(i/X);

if(!ar[ix]) {
ar[ix] = [];
}

ar[ix].push(it);

return ar;
}, [])

// result: [["a","b"], ["c","d"]]```

splice方法：

```var chunks = function(array, size) {
var results = [];
while (array.length) {
results.push(array.splice(0, size));
}
return results;
};```

```Array.prototype.chunk = function ( n ) {
if ( !this.length ) {
return [];
}
return [ this.slice( 0, n ) ].concat( this.slice(n).chunk(n) );
};

[1,2,3,4,5,6,7,8,9,0].chunk(3);
> [[1,2,3],[4,5,6],[7,8,9],[0]]```

```function chunk (arr, len) {

var chunks = [],
i = 0,
n = arr.length;

while (i < n) {
chunks.push(arr.slice(i, i += len));
}

return chunks;
}

// Optionally, you can do the following to avoid cluttering the global namespace:
Array.chunk = chunk;```

https：//stackoverflow.com/a/10456344/711085

```Object.defineProperty(Array.prototype, 'chunk_inefficient', {
value: function(chunkSize) {
var array=this;
return [].concat.apply([],
array.map(function(elem,i) {
return i%chunkSize ? [] : [array.slice(i,i+chunkSize)];
})
);
}
});```

```> [1,2,3,4,5,6,7].chunk_inefficient(3)
[[1,2,3],[4,5,6],[7]]```

`Array.map`工作原理一样，其中~是串联的：

`[[1,2,3]]~[]~[]~[] ~ [[4,5,6]]~[]~[]~[] ~ [[7]]`

```Object.defineProperty(Array.prototype, 'chunk', {
value: function(chunkSize) {
var R = [];
for (var i=0; i<this.length; i+=chunkSize)
R.push(this.slice(i,i+chunkSize));
return R;
}
});
// refresh page if experimenting and you already defined Array.prototype.chunk```
```Array.range = function(n) {
// Array.range(5) --> [0,1,2,3,4]
return Array.apply(null,Array(n)).map((x,i) => i)
};

Object.defineProperty(Array.prototype, 'chunk', {
value: function(n) {

// ACTUAL CODE FOR CHUNKING ARRAY:
return Array.range(Math.ceil(this.length/n)).map((x,i) => this.slice(i*n,i*n+n));

}
});```

```> JSON.stringify( Array.range(10).chunk(3) );
[[1,2,3],[4,5,6],[7,8,9],[10]]```

```var ceil = Math.ceil;

Object.defineProperty(Array.prototype, 'chunk', {value: function(n) {
return Array(ceil(this.length/n)).fill().map((_,i) => this.slice(i*n,i*n+n));
}});```

```Object.defineProperty(Array.prototype, 'chunk', {value: function(n) {
return Array.from(Array(ceil(this.length/n)), (_,i)=>this.slice(i*n,i*n+n));
}});```