我想在特定数组中找到重复项,因为我有以下内容:
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 });
}
我想要更具体地做的是更新特定驱动程序的信息(例如带有Id的驱动程序: sajk2299o )但是,使用上面的代码,有时它会按预期工作(删除数组中的驱动程序并推送其新位置)有时它不会删除旧位置并自动添加新位置(不知何故它不尊重driver.userId == driverId
并直接转到else
)。
我相信我在这里遗漏了一些东西,但我无法弄明白。