Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 为什么可以';你不能通过引用交换2D数组的数组吗?_Javascript_Arrays - Fatal编程技术网

Javascript 为什么可以';你不能通过引用交换2D数组的数组吗?

Javascript 为什么可以';你不能通过引用交换2D数组的数组吗?,javascript,arrays,Javascript,Arrays,我有一个包含在2D数组中的矩阵: var testM = [ [0.0,2.0,3.0], [1.0,1.0,1.0], [7.0,5.0,6.0] ]; // swap row a with b function swapRows(a,b){ let temp = a.slice(); // Copy by value, not reference a = b.slice(); b = temp; } // The individual arrays are passed in

我有一个包含在2D数组中的矩阵:

var testM = [
[0.0,2.0,3.0],
[1.0,1.0,1.0],
[7.0,5.0,6.0]
];

// swap row a with b
function swapRows(a,b){
  let temp = a.slice(); // Copy by value, not reference
  a = b.slice();
  b = temp;
}

// The individual arrays are passed in:
swapRows(testM[0], testM[1]);
但是,运行上述代码后,原始2D数组保持不变。我不太熟悉javascript中的引用传递,因此非常感谢您的帮助。(注意:内联粘贴时效果很好,只是想知道为什么即使按值复制也不起作用)

编辑:
我完全理解如何交换规则的一维数组的元素;我想了解更多关于javascript中引用传递机制的信息。(我甚至不知道我是否使用了正确的术语)

您的代码不起作用,因为在函数中分配给
a
b
不会更改原始表中的引用

向函数传递对表中两个内部数组的引用。该函数创建局部变量
a
b
,每个变量都指向其中一个引用。当您交换
a
b
时,您所做的一切就是更改局部变量指向的数组引用。您没有更改原始表中的任何内容。为此,需要对表本身进行引用,以便可以交换其数组引用指向的内容。比如:

var testM=[
[0, 2, 3],
[1, 1, 1],
[7, 5, 6]
];
//将a行与b行交换
功能swapRows(表a、b){
[表[a],表[b]=[表[b],表[a]]
}
//传入的表和索引:
swapRows(testM,0,1);

console.log(testM)
您的代码不起作用,因为在函数中分配给
a
b
不会更改原始表中的引用

向函数传递对表中两个内部数组的引用。该函数创建局部变量
a
b
,每个变量都指向其中一个引用。当您交换
a
b
时,您所做的一切就是更改局部变量指向的数组引用。您没有更改原始表中的任何内容。为此,需要对表本身进行引用,以便可以交换其数组引用指向的内容。比如:

var testM=[
[0, 2, 3],
[1, 1, 1],
[7, 5, 6]
];
//将a行与b行交换
功能swapRows(表a、b){
[表[a],表[b]=[表[b],表[a]]
}
//传入的表和索引:
swapRows(testM,0,1);
console.log(testM)
Edit 不能更改基元(字符串、数字、bigint、布尔值、null、未定义和符号),因为基元是不可变的。()

如果将数组作为参数传递,则可以在函数中更改它


swapRows(testM, 0, 1);

function swapRows(arr,a,b){   // <-- arr = testM. The value is an ARRAY, therefore it can be alterate (it's not a primitive) 
  let temp = arr[a].slice(); 
  arr[a] = arr[b].slice();
  arr[b] = temp;
}

方法 您可以扩展对象或原型(此处有一些警告:)

编辑 不能更改基元(字符串、数字、bigint、布尔值、null、未定义和符号),因为基元是不可变的。()

如果将数组作为参数传递,则可以在函数中更改它


swapRows(testM, 0, 1);

function swapRows(arr,a,b){   // <-- arr = testM. The value is an ARRAY, therefore it can be alterate (it's not a primitive) 
  let temp = arr[a].slice(); 
  arr[a] = arr[b].slice();
  arr[b] = temp;
}

方法 您可以扩展对象或原型(此处有一些警告:)


将数组本身传递给函数:@PrathapG的可能重复可能重复这些不是指交换2D数组的元素-如果这是常规int数组,元素可以很好地交换。请参见:)将数组本身传递给函数:@PrathapG的可能重复可能重复那些不是在谈论交换2D数组的元素-如果这是常规int数组,元素可以很好地交换。请参见:)
Array.prototype.swapRows = function(a,b){
  let temp = this[a].slice(); // Copy by value, not reference
  this[a] = this[b].slice();
  this[b] = temp;
  return this;
}

testM.swapRows(0, 1);