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

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

问题描述

  //创建基本的线性数组
var ImgArray = new Array (4);

//为每个或线性阵列插槽创建二维数组
for(i = 0; i <4; i ++){
ImgArray [i] = new Array (4)
}

现在我想使用2'forfor循环。但我坚持如何使用循环,因为只有 ImgArray 陈述了这样的。例如;

  //将图片
加载到ImgArray中的(var i){
for ???){//我该怎么做?我作为一个数组声明什么?
/// ...
}
document.write(< br>);

$ / code>

任何建议很好的赞赏

解决方案

假设你创建的数组,循环如下所示:

  var i,j,entry,ImgArray; 

//创建数组
ImgArray = new Array(4); (i = 0; i <4; i ++){
ImgArray [i] = new Array(4);
}

//循环两个维度
for(i = 0; i< ImgArray.length; ++ i){
entry = ImgArray [i ]。
for(j = 0; j //用entry [j]
}
}

这是因为在JavaScript中没有二维数组。 (实际上,即使数组也不是真正的数组,但让我们不去那里。)有数组,一个数组条目可以是另一个数组,但是一个数组条目可能比其他数组更长或更短。所以你检索这个数组并且通过它的长度来循环,这可能与其他维中的不同。



请注意,我没有在上面使用 for..in 。除非你真的知道自己在做什么,否则不要使用 for..in 来遍历数组。这里详情。 (如果你真的知道你在做什么,并且采取了足够的预防措施,那没问题,但是你引用的代码没有采取必要的预防措施。) for..in not 遍历数组的索引,它枚举一个对象的属性名称。

关闭-topic#1 :在JavaScript中,约定(您可以自由忽略)仅对构造函数使用初始大写( ImgArray )。 / p>

焦点#2 :您可以使用数组文字( [entry,entry,entry] code>)而不是新的Array(...),但这取决于你正在做的事情。


$ b $ (#):这是一个非常糟糕的主意,依靠分号插入(如同你的 ImgArray [i] = new Array(4)行)。确保把分号放在需要的地方,否则你会发现你不能正确地缩小你的脚本,并且/或者你会打击浪费你时间的奇怪的错误。 : - )

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

解决方案

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

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

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