我有一些代码,使用forEach循环浏览一个用户数组。在这个循环中,我再次使用forEach来循环他们的价格警报。它看起来像这样
user_data.users.forEach((user, index) => {
if (user.id == id) {
user.price_alerts.forEach((alert, alert_index) => {
if (alert.symbol == symbol) {
//remove alert
user.price_alerts.splice(alert_index, 1);
}
});
}
});
但问题是,它没有删除所有与 "SYMBOL "匹配的匹配,而是只删除了一个。 先谢谢你<3
如果你需要删除所有不满足某些条件的元素,那么尝试使用 filter
方法。
let result = user.price_alerts.filter(alert => alert.symbol !== symbol);
UPDATE:
user.price_alerts = user.price_alerts.filter(alert => alert.symbol !== symbol);