How to convert simple array into two-dimensional array (matrix) with Javascript

后端 未结 15 1686
独厮守ぢ
独厮守ぢ 2020-11-27 04:26

Imagine I have an array:

A = Array(1, 2, 3, 4, 5, 6, 7, 8, 9);

And I want it to convert into 2-dimensional array (matrix of N x M), for ins

相关标签:
15条回答
  • 2020-11-27 04:58
    function changeDimension(arr, size) {  
      var arrLen = arr.length;
      var newArr = [];
      var count=0;
      var tempArr = [];
    
      for(var i=0; i<arrLen; i++) {
        count++;
        tempArr.push(arr[i]);
    
        if (count == size || i == arrLen-1) {
          newArr.push(tempArr);
          tempArr = [];
          count = 0;
        }    
      }  
      return newArr;
    }
    
    changeDimension([0, 1, 2, 3, 4, 5], 4);
    
    0 讨论(0)
  • 2020-11-27 05:00

    How about something like:

    var matrixify = function(arr, rows, cols) {
        var matrix = [];
        if (rows * cols === arr.length) {
            for(var i = 0; i < arr.length; i+= cols) {
                matrix.push(arr.slice(i, cols + i));
            }
        }
    
        return matrix;
    };
    
    var a = [0, 1, 2, 3, 4, 5, 6, 7];
    matrixify(a, 2, 4);
    

    http://jsfiddle.net/andrewwhitaker/ERAUs/

    0 讨论(0)
  • 2020-11-27 05:05

    An awesome repository here .

    • api : masfufa.js

    • sample : masfufa.html

    According to that sample , the following snippet resolve the issue :

      jsdk.getAPI('my');
      var A=[1, 2, 3, 4, 5, 6, 7, 8, 9];
      var MX=myAPI.getInstance('masfufa',{data:A,dim:'3x3'});
    

    then :

    MX.get[0][0]  // -> 1 (first)
    MX.get[2][2] //  ->9 (last)
    
    0 讨论(0)
  • 2020-11-27 05:09
    function matrixify(array, n, m) {
        var result = [];
        for (var i = 0; i < n; i++) {
            result[i] = array.splice(0, m);
        }
        return result;
    }
    a = matrixify(a, 3, 3);
    
    0 讨论(0)
  • 2020-11-27 05:11

    This code is generic no need to worry about size and array, works universally

      function TwoDimensional(arr, size) 
        {
          var res = []; 
          for(var i=0;i < arr.length;i = i+size)
          res.push(arr.slice(i,i+size));
          return res;
        }
    
    1. Defining empty array.
    2. Iterate according to the size so we will get specified chunk.That's why I am incrementing i with size, because size can be 2,3,4,5,6......
    3. Here, first I am slicing from i to (i+size) and then I am pushing it to empty array res.
    4. Return the two-dimensional array.
    0 讨论(0)
  • 2020-11-27 05:14

    Something like this?

    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;
    }
    

    Usage:

    var matrix = listToMatrix([1, 2, 3, 4, 4, 5, 6, 7, 8, 9], 3);
    // result: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    0 讨论(0)
提交回复
热议问题