2

I have an array of arrays:

[
  [1,2],
  [2,3],
  [4,3]
]

I want to pass them to a underscore-function as a series of variables.

_.intersection([1,2],[2,3],[4,3])

How can this be done?

Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 2
    This is called argument unpacking http://stackoverflow.com/questions/7077651/python-like-unpacking-in-javascript – Stuart Sep 25 '15 at 12:34

1 Answers1

5

You can use apply

_.intersection.apply(_, myArr);
  • The first parameter to the apply method is the this context to be set on the called method.
  • The second parameter is an array whose elements will be passed as individual arguments to the called method.

MDN Docs

The apply() method calls a function with a given this value and arguments provided as an array (or an array-like object).

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • 1
    _Please_ don't encourage users to ask questions without researching them, by answering them. Obvious duplicates like these should really be closed as such. – Cerbrus Sep 25 '15 at 12:43