Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an array into a separate argument strings

Tags:

javascript

How would I get this array to be passed in as a set of strings to the function? This code doesn't work, but I think it illustrates what I'm trying to do.

var strings = ['one','two','three'];  someFunction(strings.join("','")); // someFunction('one','two','three'); 

Thanks!

like image 659
Ryan Florence Avatar asked Nov 25 '09 00:11

Ryan Florence


People also ask

Can we convert string [] to string?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.

How do you divide an array into multiple parts?

Splitting the Array Into Even Chunks Using slice() Method The easiest way to extract a chunk of an array, or rather, to slice it up, is the slice() method: slice(start, end) - Returns a part of the invoked array, between the start and end indices.


1 Answers

ES6

For ES6 JavaScript you can use the special destructuring operator :

var strings = ['one', 'two', 'three']; someFunction(...strings); 

ES5 and olders

Use apply().

var strings = ['one','two','three'];  someFunction.apply(null, strings); // someFunction('one','two','three'); 

If your function cares about object scope, pass what you'd want this to be set to as the first argument instead of null.

like image 62
Amber Avatar answered Oct 08 '22 20:10

Amber