二维数组中增强的 for 循环 - JavaScript

发布于 2024-10-07 08:20:00 字数 552 浏览 1 评论 0原文

我在 Javascript 中创建了以下 2D 数组,

// Create basic linear array
var ImgArray = new Array(4);

// Do the 2D array for each or the linear array slots
for (i=0; i < 4 ; i++) {
    ImgArray[i] = new Array(4)
}

现在我想使用 2 个“增强的 for 循环”对其进行迭代。但我一直不知道如何使用循环,因为只有 ImgArray 声明了这样的循环。例如;

// Load the images
for(var i in ImgArray) { 
    for( ??? ) {           // How would i do this? What do i state as an array?
          ///...
    }
    document.write("<br>");
}

任何建议都很感激

I created the following 2D array in Javascript

// Create basic linear array
var ImgArray = new Array(4);

// Do the 2D array for each or the linear array slots
for (i=0; i < 4 ; i++) {
    ImgArray[i] = new Array(4)
}

Now i want to iterate through it using 2 ' enhanced for loops'. But am stuck on how to use the loop as there is only ImgArray stated a such. For example;

// Load the images
for(var i in ImgArray) { 
    for( ??? ) {           // How would i do this? What do i state as an array?
          ///...
    }
    document.write("<br>");
}

Any advise well appreciated

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

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

发布评论

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

评论(3

你好,陌生人 2024-10-14 08:20:00

假设您创建了数组,循环如下所示:

var i, j, entry, ImgArray;

// Create the array
ImgArray = new Array(4);
for (i=0; i < 4 ; i++) {
    ImgArray[i] = new Array(4);
}

// Loop through both dimensions
for (i = 0; i < ImgArray.length; ++i) {
    entry = ImgArray[i];
    for (j = 0; j < entry.length; ++j) {
        // Do something with entry[j]
    }
}

这是因为 JavaScript 中没有二维数组。 (事实上​​,即使数组也不是真正的数组,但是我们不去那里。)有“数组”,一个数组条目可以是另一个数组,但一个数组条目可能比其他数组条目更长或更短。因此,您检索该数组并循环遍历其长度,该长度可能与同一“维度”中的其他数组不同。

请注意,我上面没有使用 for..in不要使用for..in来循环数组,除非你真的知道自己在做什么; 详细信息。 (如果您确实知道自己在做什么并采取了足够的预防措施,那很好,但是您引用的代码没有采取必要的预防措施。)for..in 迭代数组的索引,而是枚举对象的属性名称。

题外话#1:在 JavaScript 中,约定(您可以随意忽略)是仅对构造函数使用首字母大写 (ImgArray)。

题外话#2:您可能会考虑使用数组文字 ([entry,entry,entry]),而不是 new Array(...),但这取决于你在做什么。

题外话#3:依赖分号插入是一个非常糟糕的主意(就像您的 ImgArray[i] = new Array(4) 行一样)。确保在需要的地方放入分号,否则您会发现无法正确缩小脚本和/或您会遇到浪费时间的奇怪错误。 :-)

Assuming the array you've created, the loop looks like this:

var i, j, entry, ImgArray;

// Create the array
ImgArray = new Array(4);
for (i=0; i < 4 ; i++) {
    ImgArray[i] = new Array(4);
}

// Loop through both dimensions
for (i = 0; i < ImgArray.length; ++i) {
    entry = ImgArray[i];
    for (j = 0; j < entry.length; ++j) {
        // Do something with entry[j]
    }
}

This is because there are no two-dimensional arrays in JavaScript. (In fact, even arrays aren't really arrays, but let's not go there.) There are "arrays", and an array entry can be another array, but one array entry might be longer or shorter than others. So you retrieve that array and loop through its length, which may be different than others in the same "dimension".

Note that I didn't use for..in above. Don't use for..in to loop through arrays unless you really know what you're doing; details here. (If you do really know what you're doing and take adequate precautions, it's fine, but your quoted code isn't taking the necessary precautions.) for..in does not iterate the indexes of an array, it enumerates the property names of an object.

Off-topic #1: In JavaScript, the convention (which you're free to ignore) is to only use initial caps (ImgArray) for constructor functions.

Off-topic #2: You might look at using array literals ([entry, entry, entry]) rather than new Array(...), but it depends on what you're doing.

Off-topic #3: It's a very bad idea to rely on semicolon insertion (as with your ImgArray[i] = new Array(4) line). Make sure to put in the semicolons where they're needed, or you'll find that you can't minify your scripts properly and/or that you'll fight odd bugs that waste your time. :-)

风吹过旳痕迹 2024-10-14 08:20:00

这不是“增强的 for 循环”。无论如何,您不应该以这种方式迭代 Array 实例,至少当您将它们在语义上视为整数索引数组时。

使用您原来的

for (var i = 0; i < 4; ++i)

方法(并且不要忘记var)。也不用费心

var ImgArray = new Array(4);

var ImgArray = [];

That's not an "enhanced for loop". You should not be iterating through Array instances that way anyway, at least not when you're treating them semantically as integer-indexed arrays.

Use your original

for (var i = 0; i < 4; ++i)

approach (and don't forget var). Also don't bother with

var ImgArray = new Array(4);

Just write

var ImgArray = [];
Bonjour°[大白 2024-10-14 08:20:00

你只需要为两者做一个 for 循环

for (var i in array){
   for(var j in array[i]){//do stuff here}
}

you just have to do a for loop for both as such

for (var i in array){
   for(var j in array[i]){//do stuff here}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文