Answers for "Turn array items to matrix javascript"

0

javascript convert array to matrix

function listToMatrix(list, elementsPerSubArray) {
    var matrix = [], i, k;

    for (i = 0, k = -1; i < list.length; i++) {
        if (i % elementsPerSubArray === 0) {
            k++;
            matrix[k] = [];
        }

        matrix[k].push(list[i]);
    }

    return matrix;
}
Posted by: Guest on January-27-2021
0

Turn array items to matrix javascript

myArr.reduce((rows, key, index) => (index % 3 == 0 ? rows.push([key]) 
  : rows[rows.length-1].push(key)) && rows, []);
Posted by: Guest on August-07-2021

Code answers related to "Turn array items to matrix javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language