使用 for 循环在 JavaScript 中填充二维数组

发布于 2025-01-02 22:29:14 字数 427 浏览 0 评论 0原文

因此,我需要在 JavaScript 中填充一个二维数组,我按照以下方式执行此操作:

var i = 0, j = 0;
for (i = 0; i < roomWidth / tileWidth; i += 1) {
     roomBuffer[i] = [];
}
for (i = 0; roomWidth / tileWidth; i += 1) {
     for (j = 0; j < roomHeight / tileHeight; j += 1) {
          roomBuffer[i][j] = 1;
     }
}
alert("Hello world");

问题是它不仅不起作用,而且它后面的任何代码都不会执行。在本例中为 alert("Hello world");。我做错了什么,伙计们?

So, I need to fill a bi-dimensional array in JavaScript and I'm doing it the following way:

var i = 0, j = 0;
for (i = 0; i < roomWidth / tileWidth; i += 1) {
     roomBuffer[i] = [];
}
for (i = 0; roomWidth / tileWidth; i += 1) {
     for (j = 0; j < roomHeight / tileHeight; j += 1) {
          roomBuffer[i][j] = 1;
     }
}
alert("Hello world");

The problem is that it not only doesn't work but any code that comes after it, it's not executed. In this case the alert("Hello world");. What am I doing wrong guys?

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

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

发布评论

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

评论(4

总以为 2025-01-09 22:29:14

更改

for (i = 0; roomWidth / tileWidth; i += 1) {

for (i = 0; i < roomWidth / tileWidth; i += 1) {

change

for (i = 0; roomWidth / tileWidth; i += 1) {

to

for (i = 0; i < roomWidth / tileWidth; i += 1) {
宛菡 2025-01-09 22:29:14

您不需要在循环之前声明 ij。如果它们的存在只是为了循环,那么它们最好作为局部变量。此外,您还可以组合循环。

另外,@Yuriy Zubarev 说的是对的。 for 循环中的中间语句是在整个循环中必须保持为真的条件。

for (var i = 0; i < roomWidth / tileWidth; i++) {
     roomBuffer[i] = [];
     for (var j = 0; j < roomHeight / tileHeight; j++) {
          roomBuffer[i][j] = 1;
     }
}
alert("Hello world");

You don't need to declare i and j before the loops. If their existence is solely for the loops, they are better off as local variables. Also, you can combine loops.

Also, what @Yuriy Zubarev said is right. The middle statement in the for-loop is a condition that must hold true throughout the loop.

for (var i = 0; i < roomWidth / tileWidth; i++) {
     roomBuffer[i] = [];
     for (var j = 0; j < roomHeight / tileHeight; j++) {
          roomBuffer[i][j] = 1;
     }
}
alert("Hello world");
十级心震 2025-01-09 22:29:14

看看这个小提琴。

改变你

for (i = 0; roomWidth / tileWidth; i += 1)

for (i = 0; i < roomWidth / tileWidth; i += 1)

Have a look at this fiddle.

change your

for (i = 0; roomWidth / tileWidth; i += 1)

to

for (i = 0; i < roomWidth / tileWidth; i += 1)
时光磨忆 2025-01-09 22:29:14

您可以使用一个小的辅助函数来简化您的代码

// create an array of length len filled with value
fill = function(len, value) {
    for (var a = []; len-- > 0; a.push(value));
    return a;
}

您的代码:

size = roomWidth / tileWidth
roomBuffer = fill(size, fill(size, 1))    

You can simplify your code by using a small helper function

// create an array of length len filled with value
fill = function(len, value) {
    for (var a = []; len-- > 0; a.push(value));
    return a;
}

Your code:

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