gpt4 book ai didi

javascript - 使用js箭头函数作为参数

转载 作者:行者123 更新时间:2023-12-03 05:54:00 25 4
gpt4 key购买 nike

我正在使用箭头函数将参数传递给其他函数。它看起来有点像这样:

someFunction(parameter1, () => {
return model.associatedGroups.filter(group => {
return group.isAssociated === true;
})
}, parameter3)

但是当我调试它时,我收到了我正在调用的方法上的函数,而不是过滤后的数组。我应该如何编写它才能返回过滤后的数组?

最佳答案

您只是传递对函数的引用。

就地调用函数以将其结果作为参数传递:

someFunction(
parameter1,
(() => model.associatedGroups.filter(group => group.isAssociated === true))(),
parameter3
)

或者只传递过滤器的结果:

someFunction(
parameter1,
model.associatedGroups.filter(group => group.isAssociated === true),
parameter3
)

或者按原样传递函数并在 someFunction 中调用它以获取其结果。

关于javascript - 使用js箭头函数作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40017283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com