Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用数组移动方法更改数组中的位置?_Javascript_Arrays_Logic - Fatal编程技术网

Javascript 如何使用数组移动方法更改数组中的位置?

Javascript 如何使用数组移动方法更改数组中的位置?,javascript,arrays,logic,Javascript,Arrays,Logic,例如,我的意图是创建益智游戏 主要优先顺序是将单元格从一个具有值的单元格移动到另一个未定义的单元格 以下是我的功能: moveCell(row, col) { this.puzzleTable = []; let actRow = 0, actCol = 0; for (let r = -1; r <= 1; r++) { actRow = row + r; if (actRow >= 0 && ac

例如,我的意图是创建益智游戏

主要优先顺序是将单元格从一个具有值的单元格移动到另一个未定义的单元格

以下是我的功能:

  moveCell(row, col) {
    this.puzzleTable = [];
    let actRow = 0,
      actCol = 0;

    for (let r = -1; r <= 1; r++) {
      actRow = row + r;
      if (actRow >= 0 && actRow < this.rowCount) {

        moveCells(this.puzzleTable[actRow][col], row, actRow);
      }
    }
    for (let c = -1; c <= 1; c++) {
      actCol = col + c;
      if (actCol >= 0 && actCol < this.colCount) {

        moveCells(this.puzzleTable[row][actCol], col, actCol);

      }
    }
  }
我不想更改它们的,我希望它们交换


最佳做法是什么

您可以简单地将两个单元格的值存储在两个临时变量中,并相应地分配给彼此

例如:

Arr[0][0] to Arr[1][1]
var tempA=Arr[0][0];
var tempB=Arr[1][1];
Arr[0][0]=tempB;
Arr[1][1]=tempA;

这种交换看起来容易多了

var点=[[1,2]、[10,20]、[100200];
log(points.toString());

[点[1],点[2]]=[点[2],点[1]]如果问题已在SO中的某个位置有答案,则不在此处复制/粘贴,而是将其标记为duplicatedPossible duplicate of