JavaScript数组2维的循环 [英] JavaScript array 2 dimension for loop

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

问题描述

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

  var newLoc = []; 

var index; (i = 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] =(位置[i] [1] +位置[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];



$ / code $ / pre

位置数组本身是旧的数组存储新数组(newLoc)的数据。存在坐标经纬度的位置数据。我想我的循环表单有什么问题,或者我如何声明newLoc 2维数组,但我仍然不知道如何解决这个问题。任何帮助赞赏。

解决方案

有些事情可以通过代码进行优化。首先你必须正确地初始化你的循环。那么在循环内部,最好静态地赋值,而不是每次只检查一次。这应该优化你的代码。旧的位置发布可以使下面的代码更加优化。

  var newLoc = [];如果(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 $ b $如果(location [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];
}
}


I want to define a two dimensional array object with for loop... my problem I think my object didn't really get processed, here's the code :

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 array itself is the old array which store the data for new array (newLoc). locations' data are existing which are coordinate latitude and longitude. I suppose there is something wrong with my for loop form or how I declare newLoc 2 dimension array, but I still don't know how to fix it. Any help appreciated.

解决方案

There are some things you can do with your code for optimization. First you have to initialize your loop correctly. then inside the loop, it is better to assign the value statically rather checking it every time for only one implementation. This should optimize your code. The old location posting could make the following code more optimized.

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

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

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