James Forbes
James Forbes

Reputation: 1497

How do you partially apply a function with multiple arguments?

I want to partially apply a function with an array of arguments. When I tried to use apply() on _.partial() I got a type error in lodash.

function test(){
  return ([]).join.call(arguments,' ')
}


var p = _.partial.apply(null,test,[1,2,3]) //Type Error in lodash


p(4) //should log "1 2 3 4"

Any help with this would be greatly appreciated.

Upvotes: 2

Views: 449

Answers (1)

elclanrs
elclanrs

Reputation: 94101

You want:

_.partial(test, 1, 2, 3)
// equivalent to:
_.partial.apply(null, [test, 1, 2, 3])

Or dynamically:

var args = [1,2,3]

_.partial.apply(null, [test].concat(args))

Upvotes: 2

Related Questions