为什么x在每次迭代上等于最大长度?

发布于 2025-01-19 07:06:02 字数 1031 浏览 3 评论 0原文

我一直在尝试生成二维对象阵列。每个对象在两个变量中包含它在数组中的内部位置,称为X和Y。当我实例化对象时,我可以通过控制台记录它与适当的x和y值实例化,也可以通过事实验证。数组中的该位置与这些值完全相对应。

但是,当我在迭代迭代并保存每个位置两个位置两个位置的实例后记录最终数组时,它们具有相同的x坐标,但是y坐标是完美的。

let coordinates = Array(30).fill(Array(40)) // just creates an empty two dimensional array.

class Tile {
 constructor(x,y){
 this.x = x;
 this.y = y;
 }
}

for (let x = 0; x < coordinates.length; x++) {
 for (let y = 0; y < coordinates[x].length; y++) {
  console.log({x,y}) // logs {x: 0, y: 0} -> {x: 29, y:39}
  coordinates[x][y] = new Tile(x,y)
 }
}
console.log(coordinates) // logs two dimensional array with {x: 29, y:0} -> {x: 29, y:39}

我将在精神上疯狂,试图弄清楚为什么这样做。我已经尝试研究绑定,范围,对象而不是类,但我找不到解决方法。我相信弄清楚这将导致解决我的几个问题,因为尽管根本没有触摸它,但我遇到了实际设置的实际瓷砖类的其他属性。我指的是使用深厚的旅行和递归创建迷宫,并且每个瓷砖都包含一个称为“访问”的属性,并在一次穿越后将其设置为True。

这样做时,我遇到了我的迷宫突然停止,因为它决定了周围的每个瓷砖都被“访问”了,因为它们都等同于真实,但我从未触摸过它们。

你们会拥有的任何可以解释我遇到的东西的东西都会有所帮助,这无疑是一个真正简单而愚蠢的事情。

先感谢您。

I've been trying to generate a two dimensional array of objects. Each object contains it's own internal position in the array in two variables called X and Y. When I instantiate the object, I can see through console logging that it's being instantiated with the proper X and Y values, this can also be validated through the fact that position in the array corresponds perfectly to those values.

However, when I log the final array after iterating through and saving each position in the array two an instance of the object, they have identical X coordinates, yet the Y coordinates are perfect.

let coordinates = Array(30).fill(Array(40)) // just creates an empty two dimensional array.

class Tile {
 constructor(x,y){
 this.x = x;
 this.y = y;
 }
}

for (let x = 0; x < coordinates.length; x++) {
 for (let y = 0; y < coordinates[x].length; y++) {
  console.log({x,y}) // logs {x: 0, y: 0} -> {x: 29, y:39}
  coordinates[x][y] = new Tile(x,y)
 }
}
console.log(coordinates) // logs two dimensional array with {x: 29, y:0} -> {x: 29, y:39}

I'm going mentally crazy trying to figure out why it does that. I've tried researching bindings, scope, objects instead of classes and I cannot find a way around it. I believe figuring this out will result in solving several of my problems, as I've run into other properties on the actual Tile class being randomly set despite not touching it at all. I am referring to creating a maze using deep traveling and recursion, and having each tile contain a property called "Visited" and setting it to true once it's been traversed once.

When doing that, I run into that my maze suddenly stops as it decides that every tile around it has been "visited" as they are all equal to true, yet I've never touched them.

Anything that you guys would have that would explain what I am running into would be helpful, it's no doubt something really simple and stupid.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2025-01-26 07:06:02

for (let y = 0; x < 坐标[x].length; y++) 是你的问题。
如果您将 Y 循环与 X 进行比较,那么您的循环肯定无法工作,它永远不会消失。

for (let y = 0; x < coordinates[x].length; y++) is your problem.
Your loop definitely cannot work if you are comparing to X for your Y loops, it won't ever get out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文