38

I have the following string

output_string = "[10, 10, [1,2,3,4,5], [10,20,30,40,50]]"

Then I JSON.parse it

my_args = JSON.parse(output_string)

How do I unpack it in a Python-like way so that every element in my_args becomes an argument to a JavaScript function?

some_javascript_function(*my_args)
// should be equivalent to:
some_javascript_function(my_args[0],my_args[1],my_args[2],my_args[3])
// or:
some_javascript_function(10, 10, [1,2,3,4,5], [10,20,30,40,50])

Is there a core JavaScript idiom that does that?

Kit
  • 30,365
  • 39
  • 105
  • 149
  • Duplicate of http://stackoverflow.com/questions/3422458/unpacking-array-into-separate-variables-in-javascript – arunkumar Aug 16 '11 at 11:45
  • @arunkumar, [this answer](http://stackoverflow.com/questions/3422458/unpacking-array-into-separate-variables-in-javascript/3422473#3422473) to that question looks interesting, which makes this a slightly different question. Can we do that for function arguments? – Kit Aug 16 '11 at 11:50
  • My apologies, you are right. It is not a duplicate. And there seems to be a way to do this in the answer below. I will remove the earlier comment, since its not relevant. – arunkumar Aug 16 '11 at 11:56

3 Answers3

42

Once you 've collected the function arguments in an array, you can use the apply() method of the function object to invoke your predefined function with it:

   some_javascript_function.apply(this, my_args)

The first parameter (this) sets the context of the invoked function.

fbuchinger
  • 4,494
  • 2
  • 30
  • 31
  • 2
    `.apply` works well, but `.call` doesn't quite hit it with an `Array` argument. Thanks, you've pointed me in the right direction. This article, [Function apply and function call in JavaScript](http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx) looks interesting. – Kit Aug 16 '11 at 12:02
  • 2
    Note this won't work for `console.log.apply(...)`. That should use `console.log.apply(console, arguments)`. See [this question](http://stackoverflow.com/questions/8159233/typeerror-illegal-invocation-on-console-log-apply). – 4Z4T4R Oct 14 '14 at 01:55
41

You can achieve that by doing this some_javascript_function(...my_args)

This is called spread operation (as unpacking is in python). view docs here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator

ifedapo olarewaju
  • 3,031
  • 1
  • 21
  • 25
12

Unpack using "..."

The same way you accept unlimited args, you can unpack them.

let vals = [1, 2, 'a', 'b'];

console.log(vals);    // [1, 2, "a", "b"]
console.log(...vals); // 1 2 "a" "b"

Example: Accept unlimited arguments into a function

It will become a list

const someFunc = (...args) => {
    console.log(args);    // [1, 2, "a", "b"]
    console.log(args[0]); // 1
    console.log(...args); // 1 2 "a" "b"
}

someFunc(1, 2, 'a', 'b');

Example: Send list of arguments into a function

const someFunc = (num1, num2, letter1, letter2) => {
    console.log(num1);    // 1
    console.log(letter1); // "a"
}

let vals = [1, 2, 'a', 'b'];
someFunc(...vals);

Send arguments

joshuakcockrell
  • 5,200
  • 2
  • 34
  • 47