Remove Duplicates from an Array of GeoFire Objects

  • A+
Category:Languages

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"} 

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, ...

This is what I want to get:

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); 

Also, it didn't work..

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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: