destructure arguments into a function call with javascript
I have a function that takes a variable number of parameters and I have those parameters in an array. The function doesn't take the array as a parameter. What I want to do is destructure the array into the arguments of the function call. I am not sure how long the array is but I know it will be more than 3.
var array = ['a', 'b', 'c', 'd', ...etc]
doit(arr[0], arr[1], arr[2], ...etc)
function doit( a, b, ...c){
//do stuff
}
javascript arrays
add a comment |
I have a function that takes a variable number of parameters and I have those parameters in an array. The function doesn't take the array as a parameter. What I want to do is destructure the array into the arguments of the function call. I am not sure how long the array is but I know it will be more than 3.
var array = ['a', 'b', 'c', 'd', ...etc]
doit(arr[0], arr[1], arr[2], ...etc)
function doit( a, b, ...c){
//do stuff
}
javascript arrays
add a comment |
I have a function that takes a variable number of parameters and I have those parameters in an array. The function doesn't take the array as a parameter. What I want to do is destructure the array into the arguments of the function call. I am not sure how long the array is but I know it will be more than 3.
var array = ['a', 'b', 'c', 'd', ...etc]
doit(arr[0], arr[1], arr[2], ...etc)
function doit( a, b, ...c){
//do stuff
}
javascript arrays
I have a function that takes a variable number of parameters and I have those parameters in an array. The function doesn't take the array as a parameter. What I want to do is destructure the array into the arguments of the function call. I am not sure how long the array is but I know it will be more than 3.
var array = ['a', 'b', 'c', 'd', ...etc]
doit(arr[0], arr[1], arr[2], ...etc)
function doit( a, b, ...c){
//do stuff
}
javascript arrays
javascript arrays
asked Nov 21 '18 at 7:08
user10302261
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Sounds like you should just spread array
into the call of doit
:
doit(...array);
Note that this is called spread (or "spread syntax"), which is not destructuring. Destructuring involves either creating a new variable, or assigning to an existing variable.
The older method would be to use apply
:
doit.apply(undefined, array);
1
@ CapitalJusticeWarrior - With your existing declaration (function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element asa
, the second asb
, and the rest of them in as an array inc
(that declaration forc
is called a rest parameter).
– T.J. Crowder
Nov 21 '18 at 7:13
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
add a comment |
One way would be to use the special variable arguments
inside any javascript function. You will gain access to all arguments, regardless of their count.
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406892%2fdestructure-arguments-into-a-function-call-with-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sounds like you should just spread array
into the call of doit
:
doit(...array);
Note that this is called spread (or "spread syntax"), which is not destructuring. Destructuring involves either creating a new variable, or assigning to an existing variable.
The older method would be to use apply
:
doit.apply(undefined, array);
1
@ CapitalJusticeWarrior - With your existing declaration (function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element asa
, the second asb
, and the rest of them in as an array inc
(that declaration forc
is called a rest parameter).
– T.J. Crowder
Nov 21 '18 at 7:13
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
add a comment |
Sounds like you should just spread array
into the call of doit
:
doit(...array);
Note that this is called spread (or "spread syntax"), which is not destructuring. Destructuring involves either creating a new variable, or assigning to an existing variable.
The older method would be to use apply
:
doit.apply(undefined, array);
1
@ CapitalJusticeWarrior - With your existing declaration (function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element asa
, the second asb
, and the rest of them in as an array inc
(that declaration forc
is called a rest parameter).
– T.J. Crowder
Nov 21 '18 at 7:13
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
add a comment |
Sounds like you should just spread array
into the call of doit
:
doit(...array);
Note that this is called spread (or "spread syntax"), which is not destructuring. Destructuring involves either creating a new variable, or assigning to an existing variable.
The older method would be to use apply
:
doit.apply(undefined, array);
Sounds like you should just spread array
into the call of doit
:
doit(...array);
Note that this is called spread (or "spread syntax"), which is not destructuring. Destructuring involves either creating a new variable, or assigning to an existing variable.
The older method would be to use apply
:
doit.apply(undefined, array);
answered Nov 21 '18 at 7:09
CertainPerformanceCertainPerformance
91.1k165179
91.1k165179
1
@ CapitalJusticeWarrior - With your existing declaration (function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element asa
, the second asb
, and the rest of them in as an array inc
(that declaration forc
is called a rest parameter).
– T.J. Crowder
Nov 21 '18 at 7:13
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
add a comment |
1
@ CapitalJusticeWarrior - With your existing declaration (function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element asa
, the second asb
, and the rest of them in as an array inc
(that declaration forc
is called a rest parameter).
– T.J. Crowder
Nov 21 '18 at 7:13
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
1
1
@ CapitalJusticeWarrior - With your existing declaration (
function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element as a
, the second as b
, and the rest of them in as an array in c
(that declaration for c
is called a rest parameter).– T.J. Crowder
Nov 21 '18 at 7:13
@ CapitalJusticeWarrior - With your existing declaration (
function doit(a, b, ...c)
), which is valid even if you may have just meant it as shorthand, if you do the above you'd get the first array element as a
, the second as b
, and the rest of them in as an array in c
(that declaration for c
is called a rest parameter).– T.J. Crowder
Nov 21 '18 at 7:13
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
Thanks! I was only thinking of using the spread within array brackets.
– user10302261
Nov 21 '18 at 7:17
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
@CapitalJusticeWarrior Note that compatibility, while high, is not uniform, see MDN and for rest too. (Probably not an issue if spread works in your process already, but you might consider Babel just to be safe, if you aren't already, if you want to support ancient browsers)
– CertainPerformance
Nov 21 '18 at 7:21
add a comment |
One way would be to use the special variable arguments
inside any javascript function. You will gain access to all arguments, regardless of their count.
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
add a comment |
One way would be to use the special variable arguments
inside any javascript function. You will gain access to all arguments, regardless of their count.
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
add a comment |
One way would be to use the special variable arguments
inside any javascript function. You will gain access to all arguments, regardless of their count.
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
One way would be to use the special variable arguments
inside any javascript function. You will gain access to all arguments, regardless of their count.
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
var arr2 = ['A', 'B', 'C'];
doit(arr1); // one argument: [a,b,c,d,e,f]
doit(arr2); // one argument: [A,B,C]
doit(arr1[0], arr2[1], 'anything', -3.1); // four arguments: a, B, 'anything', -3.1
function doit(){
for(var i=0; i<arguments.length; i++) {
console.log("argument " + i + " is : " + arguments[i]);
}
}
answered Nov 21 '18 at 7:32
AhmadAhmad
8,30043664
8,30043664
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406892%2fdestructure-arguments-into-a-function-call-with-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown