即使返回false,Javascript过滤器方法也会返回原始数组
我有一个过滤方法如下:
let newArray = Alerts[symbol].filter(async (alert: IDiscordSymbolAlert, alert_index: number) => {
// If Price when set is greater than alert price, price has to move UP to trigger
if (alert.price_when_set < alert.alert_price) {
hasToGo = Change.UP;
}
// If Price when set is greater than alert price, price has to move DOWN to trigger
else if (alert.price_when_set > alert.alert_price){
hasToGo = Change.DOWN;
}
/**If the hasToGo is UP and current price is greater than alert price, then ALERT USER [OR]
// If the hasToGo is DOWN and current price is lesser than alert price, then ALERT USER **/
if((hasToGo === Change.UP && (price >= alert.alert_price)) ||
(hasToGo === Change.DOWN && (price <= alert.alert_price))) {
/**
* Send Notification that alert has been hit
*/
let userToSend = await discord.users.fetch(alert.userID)
if(!userToSend) return;
//@ts-ignore
userToSend.send(`> @everyonenn${symbol} has reached your alert price - ${alert.alert_price}`);
// remove that alert
guild_modified = true;
return false;
}
return true;
});
// When logged here, it shows the entire array even though certain values have returned false.
// I know they returned false because the block in which they returned false was executed and
//i did receive a notification which was executed in the same block.
console.log(newArray);
在此处记录时(console.log),即使某些值返回 false,它也会显示整个数组。我知道他们返回 false 是因为他们返回 false 的块被执行了,我确实收到了在同一个块中执行的通知。我知道这与 有关async/await,但我不确定是什么。有人可以看看吗。
更新的问题
Alerts[symbol].forEach(async (alert: IDiscordSymbolAlert, alert_index: number) => {
// If Price when set is greater than alert price, price has to move UP to trigger
if (alert.price_when_set < alert.alert_price) {
hasToGo = Change.UP;
}
// If Price when set is greater than alert price, price has to move DOWN to trigger
else if (alert.price_when_set > alert.alert_price){
hasToGo = Change.DOWN;
}
/**If the hasToGo is UP and current price is greater than alert price, then ALERT USER [OR]
// If the hasToGo is DOWN and current price is lesser than alert price, then ALERT USER **/
if((hasToGo === Change.UP && (price >= alert.alert_price)) ||
(hasToGo === Change.DOWN && (price <= alert.alert_price))) {
/**
* Send Notification that alert has been hit
*/
let userToSend = await discord.users.fetch(alert.userID)
if(!userToSend) return;
//@ts-ignore
userToSend.send(`> @everyonenn${symbol} has reached your alert price - ${alert.alert_price}`);
// remove that alert
Alerts![symbol].splice(alert_index, 1);
guild_modified = true;
}
});
console.log(Alerts[symbol]);
由于过滤器不能在这种情况下与async/await. 这是我使用forEach()循环并使用splice(). 但是由于 forEach 本身是异步的,并且console.log在异步块之外起作用。它不返回新数组,而是返回未修改的数组。如何返回要在 forEach 块范围之外使用的新数组。
提前致谢!
回答
您不能将async函数与filter. filter返回回调为其返回真值的所有元素。async函数总是返回承诺,它们是对象,所有非null对象都是真实的。
如果您的filter支票需要async,则您的整个操作都async需要这样处理。
这是一个即用型async过滤功能,可并行执行元素检查。同样,与所有async函数一样,它返回其结果的承诺:
async function filterAsync(array, callback, thisArg = undefined) {
const flags = await Promise.all(array.map(callback, thisArg));
const result = [];
for (let n = 0; n < array.length; ++n) {
if (flags[n]) {
result.push(array[n]);
}
}
return result;
}
要使用它,您必须使用承诺:
// In an `async` function (assuming you want errors to propagate):
const newArray = await filterAsync(Alerts[symbol], element => /*...keep-or-toss logic...*/);
// ...use `newArray`...
或者
// Not in an `async` function
filterAsync(Alerts[symbol], element => /*...keep-or-toss logic...*/)
.then(newArray => {
// ...use `newArray`...
})
.catch(error => {
// ...handle/report error...
});