Convert an indefinite number of arrays to a constant number of two-dimensional arrays in JavaScript

Convert an indefinite number of arrays to a constant number of two-dimensional arrays in JavaScript

The following is an example of making the data array into an array of four arrays from the beginning and an array at the end as well.

const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const results = [];
while (data.length) results.push(data.splice(0, 4)); // Just change the value here.
console.log(results.length); // 3
console.log(results); // [Array(4), Array(4), Array(2)]

JavaScriptで不定な配列を一定数の二次元配列に変換する

コメント

タイトルとURLをコピーしました