我在java脚本数组中有以下lat / long集合。
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]];
现在我想从这个数组中找到重复的lat / long并生成包含重复数组列表的新数组。
我的代码:
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);
}
但是没有产生结果,因为我希望它每次都是唯一的重复项吗?
答案 0 :(得分:0)
请尝试以下代码:
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]];
var dupArr = []
var templocData = locData.join("|");
$.each(locData, function(index, value) {
//console.log(index + ': ' + value);
//console.log($.inArray( value , locData ));
var searchVal = ""+value+"";
//console.log(countInstances(templocData,searchVal));
//console.log(templocData);
//console.log(searchVal);
if ( countInstances(templocData,searchVal) >= 1 && $.inArray( searchVal, dupArr) == -1 ) {
dupArr.push(searchVal);
}
});
function countInstances(string, word) {
var substrings = string.split(word);
return substrings.length - 1;
}
console.log(dupArr);
因此,结果将是:
["32.0770488,34.7958072", "32.0776696,34.7683079", "32.07849,34.773982", "32.077795,34.774825", "32.077589,34.774565", "32.083678,34.775796", "32.079743,34.770179", "32.115119,34.818128", "31.749309,35.236047"]
答案 1 :(得分:0)
试试此代码
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);
您的结果将是
[[32.0776696,34.7683079]
[32.07849,34.773982]
[32.077795,34.774825]
[32.077589,34.774565]
[32.079743,34.770179]]