dongposhi8677 2015-01-01 06:35
浏览 64
已采纳

在jQuery中从数组中找到重复的lat long

I have following collection of lat/long in java-script array.

locData=[[32.0770488,34.7958072],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.07849,34.773982],[32.07849,34.773982],[32.07849,34.773982],[32.07849,34.773982],[32.07849,34.773982],[32.07849,34.773982],[32.07849,34.773982],[32.07849,34.773982],[32.077795,34.774825],[32.077795,34.774825],[32.077589,34.774565],[32.077589,34.774565],[32.077795,34.774825],[32.077795,34.774825],[32.077589,34.774565],[32.077795,34.774825],[32.077589,34.774565],[32.077795,34.774825],[32.077795,34.774825],[32.077589,34.774565],[32.077589,34.774565],[32.077589,34.774565],[32.077589,34.774565],[32.077589,34.774565],[32.083678,34.775796],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.079743,34.770179],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.0776696,34.7683079],[32.115119,34.818128],[31.749309,35.236047]];

Now I want to find duplicate lat/long from this array and generate new array which contains duplicate array list.

My Code:

for (var i = 0; i < locData.length; i++) {
        var title = JSON.stringify(titleData[i]);
        var id = JSON.stringify(ids[i]);
        title = title.replace(/[[\]/"]/g, "");
        id = id.replace(/[[\]/"]/g, "");
        var count = 0;
        var multiArray = [];    

        for(var j=0; j<locData.length; j++){
            if(locData[i][0] === locData[j][0] && locData[i][1] === locData[j][1]){
                count++;

                if(count>1){
                    //alert(locData[i]);
                    //console.log(locData[i]);
                    multiArray.push(locData[i]);

                }

            }
        }
        console.log(multiArray); 
}

But is not generating result as I want it with duplicates with unique each time is it any way?

  • 写回答

2条回答 默认 最新

  • douquanzhan0315 2015-01-01 08:12
    关注

    Try this code

    var multiArray = [];
    for (var i = 0; i < locData.length; i++) {
    
            var count = 0;
            for(var j=0; j<locData.length; j++){
                if(locData[i][0] === locData[j][0] && locData[i][1] === locData[j][1]){
                    count++;
                    if(count>1){ 
                          var mult_indx = 0;
                          for(x in multiArray)
                          {
                              if(locData[j][0] == multiArray[x][0] && locData[j][1] == multiArray[x][1])
                              {
                                  mult_indx++;
                              }
                          }
                          if(mult_indx==0)
                          {
                              multiArray.push(locData[i]);
                          }                    
                         locData.splice(j,2);
                    }              
                }
            }
    }
    
     console.log(multiArray); 
    

    Your reslut will be

    [[32.0776696,34.7683079]
     [32.07849,34.773982]
     [32.077795,34.774825]
     [32.077589,34.774565]
     [32.079743,34.770179]]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀