简体   繁体   English

了解JavaScript中的二维数组

[英]understanding 2-d arrays in javascript

I'm trying to write a web-based curve fitting program for my students. 我正在尝试为我的学生编写基于Web的曲线拟合程序。 In order to do this I need to understand how 2D arrays (arrays of arrays) work in javascript, and it is pretty clear that I don't. 为了做到这一点,我需要了解2D数组(数组的数组)如何在javascript中工作,并且很显然我没有。 In the code below, I expect the two loops that write to the console to give the same result, but they don't. 在下面的代码中,我希望写入控制台的两个循环能够获得相同的结果,但事实并非如此。 The first one gives what I expect. 第一个给出了我的期望。 The second one is mysterious to me. 第二个对我来说是神秘的。 It gives the same result as a simple console.log(q); 它给出的结果与简单的console.log(q);相同console.log(q);

Just to be clear, the solution provided in Copying array by value in JavaScript does not work. 请注意,JavaScript按值复制数组中提供的解决方案不起作用。 If I create the array c, then create q by 如果我创建数组c,则通过创建q

var q = c.slice();

when I compute the inverse of the matrix c, I find that q has also been inverted. 当我计算矩阵c的逆时,我发现q也被逆了。 I need to have both the inverted and uninverted versions of the matrix handy... 我需要方便地使用矩阵的反向和反向版本...

 var q = []; var c = []; var row = []; // create the array q for (i=0; i<4; i++) { row.push(0); } for (i=0; i<4; i++) { q.push(row); } // create the array c c.push([1, 2, 5, 1]); c.push([3, -4, 3, -2]); c.push([4, 3, 2, -1]); c.push([1, -2, -4, -1]); for (var i=0; i<4; i++) { for (var j=0; j<4; j++) { q[i][j] = c[i][j]; console.log(q[i][j]); } } console.log('-------------------------') for (var i=0; i<4; i++) { for (var j=0; j<4; j++) { console.log(q[i][j]); } } 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You're initializing every position with the same array row in memory. 您正在用内存中的相同数组row初始化每个位置。

An alternative is to initialize with a new array for each iteration. 另一种方法是为每次迭代使用新的数组进行初始化。

 var q = []; var c = []; for (i=0; i<4; i++) { q.push(new Array(4).fill(0)); } // create the array c c.push([1, 2, 5, 1]); c.push([3, -4, 3, -2]); c.push([4, 3, 2, -1]); c.push([1, -2, -4, -1]); for (var i=0; i<4; i++) { for (var j=0; j<4; j++) { q[i][j] = c[i][j]; console.log(q[i][j]); } } console.log('-------------------------') for (var i=0; i<4; i++) { for (var j=0; j<4; j++) { console.log(q[i][j]); } } 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

There is no real 2d array in js. js中没有真正的二维数组。 2d just means that the array contains references to other arrays. 2d仅表示该数组包含对其他数组的引用 In your case however, youve got only one "row". 但是,在您的情况下,您只有一个“行”。 And then you put references to that one row a few times into the outer array. 然后,将对这一行的引用几次放入外部数组中。

  row = [0, 0, 0, 0]
  q = [row, row, row, row];

Check your reference assignments. 检查您的参考作业。 Note that you've pushed the same row object on to q four times. 请注意,您已将同一行对象推到q上四次。

Arrays are passed by reference so each time you modify row you are changing same array. 数组是通过引用传递的,因此每次修改row您都在更改同一数组。 You can create shallow copy using slice() method. 您可以使用slice()方法创建浅表副本。

 var q = []; var c = []; var row = []; // create the array q for (i = 0; i < 4; i++) { row.push(0); } for (i = 0; i < 4; i++) { q.push(row.slice()); } // create the array c c.push([1, 2, 5, 1]); c.push([3, -4, 3, -2]); c.push([4, 3, 2, -1]); c.push([1, -2, -4, -1]); for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { q[i][j] = c[i][j]; } } console.log(JSON.stringify(q)) console.log(JSON.stringify(c)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM