2017-09-06 37 views
1

目前我正在从设备坐标(纬度,经度)和存储推动他们在一个数组是类似以下内容:的JavaScript - 查找并删除重复有条件

[35.23223,-5.293222] 

不过,在几次这些坐标得到复制(也许是设备发送相同COORDS,等...)

对于这一点,我已经实现了以下:

var uniqueCoords = [Array.from(new Set(coords))]; 

其中在每次调用,挖到现有的数组中并删除任何重复的坐标。

但是,这会导致严重的问题,尤其是在有(例如)新纬度和旧经度(即[35.23223,-5.319399])或反之亦然时。

在这个特殊的例子uniqueCoords将深入到阵列发现35.23223被复制和删除,并会留下-5.319399独自一人,由旅程可能结束其结束:

[35.23223,-5.293222,-5.319399] 

我想在这里什么只有当两者对(lat & long)与数组中已存在的一对完全相同时才去除(经纬/长整数)。

当前代码:

this.storage.get('route').then((route) => { 

    let uniqueCoords: any = [Array.from(new Set(route))]; 

     uniqueCoords.push(latLng.lat, latLng.lng); 
     this.storage.set('routeTaken', uniqueCoords); 

    }).catch((error) => { 
    this.presentAlert(error) 
    }) 

阵列的原始数据:

[35.7790733,-5.8453983,35.779335,-5.8465283,35.779705,-5.84782,35.7787533,-5.8482083,35.7780167,-5.8491983,35.77782,-5.8504883,35.7774783,-5.8518267,35.776955,-5.852945,35.7765,-5.8541383,35.7761667,-5.855425,-5.8566467,35.77628,-5.8579367,35.7763233,-5.8588633,35.776435,-5.8591367,35.7767667,-5.8594817,35.7776267,-5.8586933,35.7785467,-5.8577233,-5.8585467,35.77949,-5.8597567,35.7797183,-5.86081,35.7805917,-5.8606533,35.7817533,-5.8606867,35.7826217,-5.8618667,35.78295,-5.8636367,35.7834217,-5.8643667] 
+0

所以每次你是推2个单元没有华丽的狗屎呢? lat和long – marvel308

+0

是的,确切的! –

+1

你可以在数组中插入数组吗?所以你可以使用[[35.3,-5.2],[32.2,-5.9]]等等来保持对,这会使得它比只填充数字的数组更具可读性 – adeneo

回答

0

这里是一个非常合乎逻辑的,一步一步,这样做

var coords = [ 
 
    35.7790733, -5.8453983, 
 
    35.779335, -5.8465283, 
 
    35.7790733, -5.8453983, 
 
    35.779705, -5.84782 
 
]; 
 
var temp = []; 
 
var unique = []; 
 
var uniqueCoords = []; 
 

 
for (var i = 0; i < coords.length; i += 2) { 
 
    temp.push(coords[i] + '---' + coords[i + 1]); // create some strings 
 
} 
 

 
for (var i = 0; i < temp.length; i++) { 
 
    if (unique.indexOf(temp[i]) === -1)   // remove duplicates 
 
    unique.push(temp[i]); 
 
} 
 

 
for (var i = 0; i < unique.length; i++) {  // split the strings back into array 
 
    uniqueCoords = uniqueCoords.concat(unique[i].split('---')); 
 
} 
 

 
console.log(uniqueCoords)

+0

这实际上是一个非常糟糕的解决方案,因为它的计算复杂性是n2。 –

+0

@MarcinMalinowski - 然而,我敢打赌你好钱这wawaay比其他答案更快 – adeneo

+0

只有几个coords它都一样,但添加10k的绳子,然后比较 –

2

您可以加入坐标和uniqueifying之后他们分开。

var coordinates = [[35.23223, -5.293222], [35.23223, -5.319399], [35.23223, -5.319399]], 
 
    unique = Array.from(new Set(coordinates.map(a => a.join('|'))), s => s.split('|').map(Number)); 
 
    
 
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果拍摄点的阵列,如[35.23223, -5.293222],你插入点的对象。另一个具有相同坐标的pont生成一个新的对象,它不等于前一个数组。为了使两者相等,你需要对数组进行一些处理。这可能是一个JSON字符串,或者更简单一些,如使用分隔符连接。


与单个数组中的连续坐标相同。

var coordinates = [35.23223, -5.293222, 35.23223, -5.319399, 35.23223, -5.319399], 
 
    unique = Array.from(new Set(coordinates 
 
     .reduce((r, a, i) => (i % 2 ? r[r.length - 1].push(a) : r.push([a]), r), []) 
 
     .map(a => a.join('|'))), s => s.split('|').map(Number)); 
 
    
 
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

不仅答案很好,而且SO控制台输出的样式是我以前从未见过的漂亮技巧。做得好! – Santi

+0

这是一个很好的答案,但我想了解更多发生了什么,你能否详细说明一下? –

+0

这个答案假设输入格式改变 –

0

假设uniqueCoords被格式化为[lat,long,lat,long,...]new Set产生类似的东西[lat, long]

let coordSet = Array.from(new Set(coords)); 

// search the uniqueCoords list 
let exists = uniqueCoords.find(function (itemCoord, idx, arr) { 

    // only check on every other coord 
    // if the previous coord being looped over matches the first coord in the coord set 
    // if the current coord being looped over matches the second coord in the coord set 
    // return true indicating the coord-set exists 
    if ((idx % 2) === 1) && (arr[idx -1] === coordSet[0]) && (itemCoord === coordSet[1]) { 
     return true; 
    } 
}); 

// if a matching coord-set wasn't found, push the new set 
if (!exists) { 
    // called with apply so each item in coordSet 
    // is appended to the uniqueCoords Array 
    uniqueCoords.push.apply(uniqueCoords, coordSet); 
} 
+0

请指出为什么downvote;如果它的逻辑错误,请说明为什么 – SReject

0

如果你只是切换到['lat,lng', ...]的格式(作为字符串)你的代码将工作精细。

但是你应该在之后检查重复的以增加新的坐标。目前您在之前

0
this.storage.get('route').then((route) => { 

if (route.length < 2 || route[route.length - 2] != latLng.lat || route[route.length - 1] != latLng.lng) { 
     uniqueCoords.push(latLng.lat, latLng.lng); 
     this.storage.set('routeTaken', uniqueCoords); 
} 

    }).catch((error) => { 
    this.presentAlert(error) 
    })