【问题标题】:JavaScript array 2 dimension for loopJavaScript 数组二维循环
【发布时间】:2016-11-01 11:08:41
【问题描述】:

我想用 for 循环定义一个二维数组对象...我的问题我认为我的对象并没有真正得到处理,代码如下:

var newLoc = [];

var index;

for (index = 0, i < locations.length; i++){
                if(i == 0) {
                    newLoc[i][0] = locations[i][1];
                    newLoc[i][1] = locations[i][2];
                }
                else {
                    if(locations[i][8] == locations[i-1][8]){
                        newLoc[i-1][0] = (locations[i][1] + locations[i-1][1])/2;
                        newLoc[i-1][1] = (locations[i][2] + locations[i-2][1])/2;    
                    }                            
                    else{
                        newLoc[i][0] = locations[i][1];
                        newLoc[i][1] = locations[i][2];
                    }
                }
            }

locations 数组本身是旧数组,用于存储新数组 (newLoc) 的数据。位置的数据是存在的,它们是坐标纬度和经度。我想我的 for 循环形式或我如何声明 newLoc 二维数组有问题,但我仍然不知道如何修复它。任何帮助表示赞赏。

【问题讨论】:

  • for (index = 0, i &lt; locations.length; i++){ 应该是for (var i = 0; i &lt; locations.length; i++){ 对吧? (注意;
  • 您声明了变量索引,但在 for 循环中使用 i 进行迭代。应该是:var i; for(i = 0; i &lt; locations.length; i++) {...}。我不明白它应该如何工作。
  • 感谢指出:)

标签: javascript arrays for-loop


【解决方案1】:

您可以对代码执行一些优化操作。首先,您必须正确初始化循环。然后在循环内部,最好静态分配值,而不是每次只检查一个实现。这应该优化您的代码。旧的位置发布可以使以下代码更加优化。

var newLoc = [];

if(locations.length > 0){
    for(var j = 0; j < 1; ++j) {
        newLoc[j] = [ ];

        newLoc[j][0] = locations[0][1]; 
        newLoc[j][1] = locations[0][2];

    }

}

for (var i = 1, i < locations.length; i++){

    if(locations[i][8] == locations[i-1][8]){
        newLoc[i-1][0] = (locations[i][1] + locations[i-1][1])/2;
        newLoc[i-1][1] = (locations[i][2] + locations[i-2][1])/2;    
    }                            
    else{
        newLoc[i][0] = locations[i][1];
        newLoc[i][1] = locations[i][2];
    }                
}

【讨论】:

  • 谢谢,但是你知道如何检查newLoc的值吗?
  • 与检查位置值的方式相同。我猜它会像 if(locations.length > 0)
  • 与检查位置值的方式相同。我猜它会像 if(newLoc.length > 0) –
  • 哈哈哈......对不起,我不明白。只需使用 console.log(newLoc) 在控制台中检查它。不要忘记给我投票:P Good Day..:)
  • 定义var newLoc = []; 将在newLoc[0][0] 上引发错误。索引0 处没有任何内容
【解决方案2】:

我认为问题在于 newLoc 始终是一维数组,您在 for 循环中声明了“索引”,但在正文中使用了“i”

var newLoc = [];

// loop with 
for (var i = 0; i < locations.length; i++){
    //Create the second dimention
    newLoc[i] = [];
            if(i == 0) {
                    ...

【讨论】:

  • 好吧,还是不行....有谁知道如何查看 newLoc 的值?检查它是否已经在数组中定义了值
【解决方案3】:

我注意到一件事,在你的for 循环中,你写了

for (index = 0, i < locations.length; i++)

而不是

for (index = 0; i < locations.length; i++)

注意;

所以,在所有的循环中应该是

for (index = 0; i < locations.length; i++){
            if(i == 0) {
                newLoc[i][0] = locations[i][1];
                newLoc[i][1] = locations[i][2];
            }
            else {
                if(locations[i][8] == locations[i-1][8]){
                    newLoc[i-1][0] = (locations[i][1] + locations[i-1][1])/2;
                    newLoc[i-1][1] = (locations[i][2] + locations[i-2][1])/2;    
                }                            
                else{
                    newLoc[i][0] = locations[i][1];
                    newLoc[i][1] = locations[i][2];
                }
            }
        }

【讨论】:

    【解决方案4】:

    您正在初始化一个一维数组,因此,当您遍历循环时,您的 javascript 代码可能会中断。

    尝试用这种方式初始化:

    var newLoc = new Array(locations.length).fill(locations.length);
    

    数组文档:https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2014-12-22
      • 2023-04-01
      相关资源
      最近更新 更多