从GeoFire对象数组中删除重复项 [英] Remove Duplicates from an Array of GeoFire Objects
问题描述
我正在Angular 6上使用Geofire和Firebase来存储位置,不幸的是,它存储了大量重复项,这是一个示例(控制台记录我的变量 currentHits ):
I am working with Geofire and Firebase on Angular 6 to store locations and unfortunately it's storing a lot of duplicates this is an example (console logging my variable currentHits):
0: {location: Array(2), distance: "48.84", url: "assets/imgs/fix.png"}
1: {location: Array(2), distance: "48.84", url: "assets/imgs/fix.png"}
2: {location: Array(2), distance: "48.84", url: "assets/imgs/fix.png"}
3: {location: Array(2), distance: "48.85", url: "assets/imgs/free.png"}
4: {location: Array(2), distance: "48.85", url: "assets/imgs/free.png"}
5: {location: Array(2), distance: "48.85", url: "assets/imgs/free.png"}
6: {location: Array(2), distance: "48.87", url: "assets/imgs/low.png"}
7: {location: Array(2), distance: "48.87", url: "assets/imgs/low.png"}
8: {location: Array(2), distance: "48.87", url: "assets/imgs/low.png"}
位置基本上是用于计算距离的经度和纬度数组,在id 0、1和2中,其坐标相同,而3,4和5也相同,...
Location basically is an array of latitude and longitude used to calculate distance, in id 0, 1 and 2 its the same coordinates, and 3,4 and 5 are also the same, ...
这就是我想要得到的:
0: {location: Array(2), distance: "48.84", url: "assets/imgs/fix.png"}
1: {location: Array(2), distance: "48.85", url: "assets/imgs/free.png"}
2: {location: Array(2), distance: "48.87", url: "assets/imgs/low.png"}
(可选)这是存储这些位置的方式:
(Optional) this is how It stores these locations:
...
hits = new BehaviorSubject([])
...
queryHits(...){
....
let hit = {
location: location,
distance: distance.toFixed(2),
url:img
}
let currentHits = this.hits.value
currentHits.push(hit)
this.hits.next(currentHits)
....
}
的确,可能已经有人问过这个问题了,我一直在研究所有类似的问题并找到了这些功能:
It's true that this question has probably already been asked and I have been digging through all the similar questions and found these functions:
1. RemoveDuplicates()
function removeDuplicates(arr){
let unique_array = []
for(let i = 0;i < arr.length; i++){
if(unique_array.indexOf(arr[i]) == -1){
unique_array.push(arr[i])
}
}
return unique_array
}
var newlist = removeDuplicates(list)
这没用,我得到了重复的相同列表.
It didn't work I get the same list with duplicates.
2. arrUnique:
function arrUnique(arr) {
var cleaned = [];
arr.forEach(function(itm) {
var unique = true;
cleaned.forEach(function(itm2) {
if (_.isEqual(itm, itm2)) unique = false;
});
if (unique) cleaned.push(itm);
});
return cleaned;
}
var newlist= arrUnique(list);
也没有用..
3. onlyUnique
onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var newlist = list.filter(onlyUnique)
不幸的是,它没有用...
Unfortunately it didn't work...
这些是针对类似问题的一些答案,这些问题可以从数组中删除重复项,但没有一个起作用.我不明白为什么它们不适用于我的数组类型,如果有人有想法或知道为什么会很有帮助.
These are some of the answers given to similar problem to remove duplicates from an array and none of them worked. I don't understand why they won't work for my type of array, If anyone has an idea or knows why would be very helpful.
推荐答案
您可以使用一组存储和检查重复值.
You could use a set to store and check for duplicate values.
const removeDuplicates = arr => {
let matches = new Set();
return arr.filter(elem => {
const {distance} = elem;
if(matches.has(distance)){
return false;
} else {
matches.add(distance);
return true;
}
})
}
请记住,使用这种方法,您可以删除距离相同但坐标不同的结果.如果这给您造成了问题,那么您还需要检查经纬度对.
Bear in mind that using this approach you may remove results where the distance is the same but the co-ordinates differ. If that causes an issue for you then you'd need to also check against the lat/lng pair.
这篇关于从GeoFire对象数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!