Javascript-在数组中查找重复项
我想在特定数组中查找重复项,为此我有以下几点:Javascript-在数组中查找重复项,javascript,Javascript,我想在特定数组中查找重复项,为此我有以下几点: 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.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
)
我相信我在这里遗漏了一些东西,但我无法找出它。可能的重复我可以看到removeDriver和pushNewDriver方法吗?@banzomaikaka我已经添加了它removeDriver()函数没有修改任何参数,只是输入错误