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

后端 未结 15 1684
独厮守ぢ
独厮守ぢ 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:49

    The cleanest way I could come up with when stumbling across this myself was the following:

    const arrayToMatrix = (array, columns) => Array(Math.ceil(array.length / columns)).fill('').reduce((acc, cur, index) => {
      return [...acc, [...array].splice(index * columns, columns)]
    }, [])
    

    where usage would be something like

    const things = [
      'item 1', 'item 2',
      'item 1', 'item 2',
      'item 1', 'item 2'
    ]
    
    const result = arrayToMatrix(things, 2)
    

    where result ends up being

    [
      ['item 1', 'item 2'],
      ['item 1', 'item 2'],
      ['item 1', 'item 2']
    ]
    
    0 讨论(0)
  • 2020-11-27 04:53

    Simplest way with ES6 using Array.from()

    const matrixify = (arr, size) => 
    Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>  
    arr.slice(i * size, i * size + size));
    const list =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ;
    console.log(matrixify(list, 3));

    0 讨论(0)
  • 2020-11-27 04:54

    Simply use two for loops:

    var rowNum = 3;
    var colNum = 3;
    var k = 0;
    var dest = new Array(rowNum);
    
    for (i=0; i<rowNum; ++i) {
      var tmp = new Array(colNum);
      for (j=0; j<colNum; ++j) {
        tmp[j] = src[k];
        k++;
      }
      dest[i] = tmp;
    }
    
    0 讨论(0)
  • 2020-11-27 04:54

    you can use push and slice like this

    var array = [1,2,3,4,5,6,7,8,9] ;
    var newarray = [[],[]] ;
    newarray[0].push(array) ;
    console.log(newarray[0]) ;
    

    output will be

    [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
    

    if you want divide array into 3 array

    var array = [1,2,3,4,5,6,7,8,9] ;
    var newarray = [[],[]] ;
    newarray[0].push(array.slice(0,2)) ;
    newarray[1].push(array.slice(3,5)) ;
    newarray[2].push(array.slice(6,8)) ;
    

    instead of three lines you can use splice

    while(array.length) newarray.push(array.splice(0,3));
    
    0 讨论(0)
  • 2020-11-27 04:55

    This a simple way to convert an array to a two-dimensional array, especially when you want to manipulate array items generated.

    const arr = [1,2,3,4,5,6,7,8,9]
    let i = 0;
    let twoDimension = [];
    while (i < arr.length) {
      const first = arr[i];
      const second = arr[i + 1];
      const third = arr[i + 2]
      let tempArr = []
    
      if (first && second && third) {
        tempArr.push(first, second, third)
      } else if (first && second) {
        tempArr.push(first, second)
      } else {
        tempArr.push(first)
      }
    
      twoDimension[twoDimension.length] = tempArr;
      i += 3;
     }
     console.log(twoDimension)
    
    0 讨论(0)
  • 2020-11-27 04:56

    Short answer use:

    const gridArray=(a,b)=>{const d=[];return a.forEach((e,f)=>{const 
    h=Math.floor(f/b);d[h]=d[h]||[],d[h][f%b]=a[f]}),d};
    

    Where:

    a: is the array
    b: is the number of columns
    

    Long answer you can read the article:

    How to convert an array of length ‘n’ to array grid

    0 讨论(0)
提交回复
热议问题