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?

3
  • Duplicate of stackoverflow.com/questions/3422458/…
    – arunkumar
    Aug 16, 2011 at 11:45
  • @arunkumar, this answer to that question looks interesting, which makes this a slightly different question. Can we do that for function arguments?
    – Kit
    Aug 16, 2011 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, 2011 at 11:56

3 Answers 3

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.

2
  • 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 looks interesting.
    – Kit
    Aug 16, 2011 at 12:02
  • 2
    Note this won't work for console.log.apply(...). That should use console.log.apply(console, arguments). See this question.
    – 4Z4T4R
    Oct 14, 2014 at 1: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

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.