简体   繁体   中英

using js arrow function as a parameter

I'm using an arrow function to pass a parameter to some other function. It looks sort of like this:

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

but when I'm debugging it, I'm receiving a function on the method i'm calling instead of a filtered array. How should I write it in order to get back a filtered array?

You are just passing reference to a function.

Call the function in place to pass its result as the argument:

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

Or just pass the result of the filter:

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

Or pass the function as you are already and call it within someFunction to get its result.

It's because you pass a function, not an array. If you want the result of this function you have to call it.

Moreover with arrow function you don't need to put `return if you have only one line.

Try with this:

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

Edit : this is enought :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM