二维数组中增强的 for 循环 - JavaScript
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您创建了数组,循环如下所示:
这是因为 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:
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 usefor..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 thannew 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. :-)这不是“增强的
for
循环”。无论如何,您不应该以这种方式迭代 Array 实例,至少当您将它们在语义上视为整数索引数组时。使用您原来的
方法(并且不要忘记
var
)。也不用费心写
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
approach (and don't forget
var
). Also don't bother withJust write
你只需要为两者做一个 for 循环
you just have to do a for loop for both as such