Short syntax for calling an array of functions with lodash

I believe a shorter way (one line) to write this with lodash:

  _.forEach(eventListeners, function(callback) {
    callback(event);
  })

      

... but not found yet

+3


source to share


1 answer


Lodash provides a utility function named _.over

that returns a function that you can then call to pass some arguments to all the functions you provided_.over

Official documentation for _.over

var funs = [
  function(e) { console.log(e) },
  function(e) { console.log(e*2) },
  function(e) { console.log(e*3) }
];

_.over(funs)(10);

      

This will call all of the array functions funs

with 10

as their argument, so in this case, you should see in your console:



10
20
30

      

In your case specifically:

_.over(eventListeners)(event);

      

+6


source







All Articles