我使用JS得到了不同的结果
filter
和
some
在我的节点js应用程序(服务器端)和浏览器控制台(客户端)中运行时的方法
我有以下两个对象数组,我想要实现的是创建一个新的对象数组,其中不包括任何重复的对象
export default async (req, res) => {
const userAlerts = req.body;
console.log(userAlerts);
userAlerts = [ {
user_id: 232,
match_id: 3918053,
alert_type: 'OVER0.5FHCORNERS',
alert_odds: '2.0',
home_team: 'Pescara U19',
},
{ user_id: 232,
match_id: 3909005,
alert_type: 'OVER0.5FHCORNERS',
alert_odds: '2.0',
home_team: 'Fortuna Koln U19',
}
]
const existingAlerts = await queries.getUserTelegramAlerts(userID);
console.log(existingAlerts);
existingAlerts = [ {
user_id: 232,
match_id: 3918053,
alert_type: 'OVER0.5FHCORNERS',
alert_odds: '2.0',
home_team: 'Pescara U19',
},
{
user_id: 232,
match_id: 3909005,
alert_type: 'OVER0.5FHCORNERS',
alert_odds: '2.0',
home_team: 'Fortuna Koln U19',
}
]
const alertsToSet = userAlerts.filter(x => {
return !existingAlerts.some(
t =>
t.user_id === x.user_id &&
t.match_id === x.match_id &&
t.alert_type === x.alert_type &&
t.alert_odds === x.alert_odds
);
});
console.log({ alertsToSet });
}
注销
alertsToSet
在浏览器中为空,这是所需的行为,但当运行此代码服务器端时,
警报ToSet
有2个对象
我是错过了什么还是犯了错误?