简体   繁体   English

如何在JavaScript中交换2D数组中的两个元素? (对我在Chrome中的console.log中看到的内容感到困惑)

[英]How to swap two elements inside of a 2D Array in JavaScript? (Confused about what I’m seeing from console.log in Chrome)

I want to swap two arrays inside a 2D array, however JS seems to be doing that before my actual swap is happening. 我想在2D数组中交换两个数组,但JS似乎在我的实际交换发生之前就这样做了。

This is for an algorithm I'm working on, providing each possible way to display a list of points. 这是我正在研究的算法,提供了显示点列表的每种可能方法。 I have tried several ways of doing this, but the key problem keeps returning, and I've managed to crop it down to this peace of code: 我已经尝试了几种方法来做到这一点,但关键问题一直在回归,我已经成功地将其简化为代码的平衡:

 var points = [[1,2],[10,20],[100,200]]; console.log(points); var a = points[1][0]; var b = points[1][1]; points[1][0] = points[2][0]; points[1][1] = points[2][1]; points[2][0] = a; points[2][1] = b; console.log(points); 

I know this code isn't close to being DRY, but it has the same problem: 'points' is logged to the console in the line after being declared, though in that log, it is already swapped? 我知道这段代码并不接近DRY,但是它也有同样的问题:声明后,行中的'points'被记录到控制台中,虽然在该日志中,它已经被交换了? How is that possible, as there were no commands yet saying it to do so. 这怎么可能,因为还没有命令说它这样做。 How does JavaScript handle this peace of code? JavaScript如何处理这种代码的和平? And why doesn't mine seem to be working? 为什么我的工作似乎没有?

**The expected output for the first log is: [[1,2],[10,20],[100,200]] **and for the second log: [[1,2],[100,200],[10,20]] **第一个日志的预期输出是:[[1,2],[10,20],[100,200]] **和第二个日志:[[1,2],[100,200],[10, 20]

The StackOverFlow snipped runs it as it is expected, but Chrome runs it differently StackOverFlow snipped按预期运行它,但Chrome运行它的方式不同

I believe what's happening here is that the console is showing something like this (in Firefox): 我相信这里发生的事情是控制台显示的内容(在Firefox中):

初始控制台

But then you click to expand it to take a look inside the array, the items are swapped: 但是,然后单击以展开它以查看数组内部,项目被交换:

扩展控制台

This is because the console is actually showing you a reference to the array, and since your algorithm modifies the original array in place , by the time you click to expand the element in console, the reference has been modified. 这是因为控制台实际上是显示你对数组的引用 ,因为你的算法就地修改原始数组,你的时间点击展开控制台的元素,参考已被修改。 Your code is fine, you might try using .toString() or JSON.stringify or even [...points] to make sure what's output to the console in each line are different values and not a reference to the same value. 您的代码很好,您可以尝试使用.toString()JSON.stringify甚至[...points]来确保每行中控制台的输出是不同的值而不是对相同值的引用。

Running your exact algorithm with console.log(points.toString()) instead, I see this (again in Firefox): 使用console.log(points.toString())运行您的确切算法,我看到了这一点(再次在Firefox中):

toString演示

And just for demonstration purposes, here's how you can simplify your code to do the same thing using destructured assignment: 仅出于演示目的,以下是如何使用结构化赋值简化代码以执行相同操作:

 var points = [[1, 2], [10, 20], [100, 200]]; console.log(points.toString()); [points[1], points[2]] = [points[2], points[1]]; console.log(points.toString()); 

pswg is correct, this has to do with Chrome's lazy eval of arrays for things like console.log. pswg是正确的,这与Chrome对console.log之类的数组的懒惰eval有关。 Here's more detail: Is Chrome's JavaScript console lazy about evaluating arrays? 这里有更多细节: Chrome的JavaScript控制台是否懒于评估数组?

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

相关问题 如何从 selenium 的 console.log 中提取数组? - How can I extract an array from the console.log in selenium? JS console.log 一个二维矩阵 - JS console.log a 2d matrix 通过对象内部的数组进行迭代,console.log可以正常工作,但是我无法使其返回react元素。 - Iterating through an array inside an object, console.log working but I can not get it to return react elements. 如何在没有 console.log 的情况下再次更新数组元素? - How to update an array elements without console.log it again? 从对象方法内部的数组中运行console.log - Running a console.log from an array thats inside a method of an object 我无法在 JavaScript 中使用 console.log 显示对象数组 - I cannot display an array of object by using console.log in JavaScript 如何将两个表单输入值获取到 Javascript console.log - How to get two form input values to Javascript console.log 如何在Handlebars模板中添加console.log()JavaScript逻辑? - How do I add console.log() JavaScript logic inside of a Handlebars template? 如何从已删除的元素中控制台记录 innerText? - How to console.log the innerText from elements that were removed? 从铬到硒的console.log - console.log from chrome to selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM