二维数组中增强的 for 循环 - JavaScript [英] Enhanced for loop in 2D Array - JavaScript

查看:16
本文介绍了二维数组中增强的 for 循环 - JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Javascript 中创建了以下二维数组

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)
}

现在我想使用 2 个增强的 for 循环"来遍历它.但是我坚持如何使用循环,因为只有 ImgArray 声明了这样的内容.例如;

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>");
}

非常感谢任何建议

推荐答案

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

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]
    }
}

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

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".

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

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.

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

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

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

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.

离题 #3:依赖分号插入是一个非常糟糕的主意(就像你的 ImgArray[i] = new Array(4) 行).确保在需要的地方加上分号,否则你会发现你不能正确地缩小你的脚本和/或你会与浪费你时间的奇怪错误作斗争.:-)

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. :-)

这篇关于二维数组中增强的 for 循环 - JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆