0

I want to find duplicates in a particular array, for that I have the following:

    this.drivers.filter((driver)=>{
      if(driver.userId == driverId){
      //this is used to updates the user location in real-time (lat, long), that's why I am removing the old location and pushing the new location
      this.removeDriver(driver.userId)
      this.pushNewDriver(lat, long, driverId);
}
else{
   //if the driver does not exists already in the array, then push it
        this.pushNewDriver(lat, long, driverId);
}
})



removeDriver(userId){
      this.drivers = this.drivers.filter((user) => {
      return user.userId !== userId;
    });
}

 pushNewDriver(lat, long, userId) {
let currentLontLat = { "lat": lat, "long": long };

 this.drivers.push({ "userId": userId, "currentLocation": currentLontLat });

}

What I want to do more specifically, is to update the info of a particular driver (e.g driver with Id : sajk2299o) however, with the code above, sometimes it works as expected (remove the driver from the array and push its new location) and sometimes it doesn't remove the old location and adds automatically the new location (somehow it does not respect driver.userId == driverId and go to else directly).

I believe that I am missing something here, but I can't figure it out.

Folky.H
  • 1,164
  • 4
  • 15
  • 37

0 Answers0