【问题标题】:Get 3x3 blocks from 9x9 2d array as String从 9x9 二维数组中获取 3x3 块作为字符串
【发布时间】:2021-07-07 20:09:05
【问题描述】:

我正在学习 Javascript,目前发现自己遇到以下问题。我需要将 9x9 2d 数组的每个单独的 3x3 块作为字符串,用逗号分隔。

我的意思是,例如,假设我有以下数组:

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

结果应该是 535671192,678195342, 912348657, ... 等,直到字符串由所有 3x3 块组成。

我认为制作嵌套 for 循环是最好的方法,但在此过程中感到困惑,非常感谢您的帮助。

谢谢。

【问题讨论】:

  • 您能分享一下您尝试过但感到困惑的代码吗?

标签: javascript arrays string


【解决方案1】:

您可以使用两个循环来遍历所有可能的左上角的位置,并使用另外两个循环来获取该正方形中的所有元素。

var arr = [
  [5, 3, 5, 6, 7, 8, 9, 1, 2],
  [6, 7, 1, 1, 9, 5, 3, 4, 8],
  [1, 9, 2, 3, 4, 2, 5, 6, 7],
  [8, 5, 9, 7, 6, 1, 4, 2, 3],
  [4, 2, 6, 5, 5, 3, 7, 9, 1],
  [7, 1, 3, 1, 2, 4, 8, 5, 6],
  [9, 6, 1, 5, 3, 7, 2, 8, 4],
  [2, 8, 7, 5, 1, 9, 6, 3, 5],
  [3, 4, 5, 6, 8, 6, 1, 7, 9]
];
let res = [];
for (let i = 0; i < 9; i += 3) {
  for (let j = 0; j < 9; j += 3) {
    let curr = "";
    for (let k = 0; k < 3; k++) {
      for (let l = 0; l < 3; l++) {
        curr += arr[i + k][j + l];
      }
    }
    res.push(curr);
  }
}
console.log(res);

【讨论】:

    【解决方案2】:

    这个问题也可以通过循环来解决,但最好使用 Array.map() 函数来解决它。

    let arrayRow = "";
      const resultArray = Array2d.map((element, index) => {
         return arrayRow + element.map((element, i) => {
      return element.toString();
      });
    });
    

    【讨论】:

      【解决方案3】:

      你也应该这样尝试。

      var arr = [
        [5, 3, 5, 6, 7, 8, 9, 1, 2],
        [6, 7, 1, 1, 9, 5, 3, 4, 8],
        [1, 9, 2, 3, 4, 2, 5, 6, 7],
        [8, 5, 9, 7, 6, 1, 4, 2, 3],
        [4, 2, 6, 5, 5, 3, 7, 9, 1],
        [7, 1, 3, 1, 2, 4, 8, 5, 6],
        [9, 6, 1, 5, 3, 7, 2, 8, 4],
        [2, 8, 7, 5, 1, 9, 6, 3, 5],
        [3, 4, 5, 6, 8, 6, 1, 7, 9]
      ];
      let res = [];
      let i = 0,j = 0;
      let justOneStr = "";
      while(i < 9 && j < 9){
        justOneStr += arr[i][j];
        if(i % 3 == 2 && j % 3 == 2){
          res.push(justOneStr); 
          justOneStr="";
        }
        
        if((j % 9 == 8 || j % 3 == 2) && i % 3 != 2){
          j -= 3;
          i += 1;
        }
        else if(j % 9 == 8 && i % 3 == 2){
          j -= 9;
          i += 1;
        }
        else if(i % 3 == 2 && j % 3 == 2){
          i -= 2;
        }
      
        j++;
      }
      
      console.log(res);

      【讨论】:

        【解决方案4】:

        这是一个相当简单的嵌套reduce(),它返回一个指定大小的矩阵数组。它接触每个元素一次,并使用传递的矩阵中的坐标来确定累积到哪个子矩阵和子矩阵行。

        const
          matrix = [
            [5, 3, 5, 6, 7, 8, 9, 1, 2],
            [6, 7, 1, 1, 9, 5, 3, 4, 8],
            [1, 9, 2, 3, 4, 2, 5, 6, 7],
            [8, 5, 9, 7, 6, 1, 4, 2, 3],
            [4, 2, 6, 5, 5, 3, 7, 9, 1],
            [7, 1, 3, 1, 2, 4, 8, 5, 6],
            [9, 6, 1, 5, 3, 7, 2, 8, 4],
            [2, 8, 7, 5, 1, 9, 6, 3, 5],
            [3, 4, 5, 6, 8, 6, 1, 7, 9]],
        
          subdivide = (matrix, width, height) => {
            return matrix.reduce((acc, row, i) => {
        
              row.reduce((_acc, x, j, row) => {
                const grid = Math.floor(j / width) + Math.floor(i / height) * (Math.ceil(row.length / width));
                const gridRow = i % height;
                _acc[grid] ??= [];
                (_acc[grid][gridRow] ??= []).push(x);
        
                return _acc;
              }, acc)
        
              return acc;
            }, [])
          },
        
          subdividedmatrix = subdivide(matrix, 3, 3);
        
        // log submatrices
        subdividedmatrix.forEach(m => console.log(JSON.stringify(m)));
        
        // map to strings
        console.log(subdividedmatrix.map(submatrix => submatrix.flat().join('')))
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-08
          • 2019-01-23
          • 2012-03-10
          • 1970-01-01
          • 1970-01-01
          • 2020-05-19
          相关资源
          最近更新 更多