turn array items to matrix javascript

Solutions on MaxInterview for turn array items to matrix javascript by the best coders in the world

showing results for - "turn array items to matrix javascript"
Nicolò
31 Jun 2020
1function listToMatrix(list, elementsPerSubArray) {
2    var matrix = [], i, k;
3
4    for (i = 0, k = -1; i < list.length; i++) {
5        if (i % elementsPerSubArray === 0) {
6            k++;
7            matrix[k] = [];
8        }
9
10        matrix[k].push(list[i]);
11    }
12
13    return matrix;
14}
15
Lilas
01 Sep 2019
1myArr.reduce((rows, key, index) => (index % 3 == 0 ? rows.push([key]) 
2  : rows[rows.length-1].push(key)) && rows, []);
3