const args = ['p0', 'p1', 'p2'];
call_me.apply(this, args);
See MDN docs for Function.prototype.apply().
If the environment supports ECMAScript 6, you can use a spread argument instead:
call_me(...args);
Answer from KaptajnKold on Stack Overflowconst args = ['p0', 'p1', 'p2'];
call_me.apply(this, args);
See MDN docs for Function.prototype.apply().
If the environment supports ECMAScript 6, you can use a spread argument instead:
call_me(...args);
Why don't you pass the entire array and process it as needed inside the function?
var x = [ 'p0', 'p1', 'p2' ];
call_me(x);
function call_me(params) {
for (i=0; i<params.length; i++) {
alert(params[i])
}
}
how do you pass an array as a parameter in a function?
Writing a function that takes an array as an argument and sums up all the elements
How to pass an array as argument to a function in javascript? - Stack Overflow
Run JS Array/Passing Arrays as Function Arguments
Videos
Hi guys I am trying to write a function that takes as an argument an array and then iterates over each element and adds them together but i am having problems, id honestly really appreciate any pointers in the right direction!
this is what I have written so far;
1) Declared y as a global binding.
2) Function takes an array as an argument- (i dont know if "(...argument)" is how you do it).
3)A loop iterates over each element of the array the range of which is 0 - length of array and increments by +1.
4)Each number that is iterated is added to y.
5) y is returned
let y = 0;
function max(...numbers){
for(let x = 0; x < length.numbers; x++){
y += x;
}
return y;
}
console.log(max([1, 2, 3]));Passing arrays to functions is no different from passing any other type:
var string = buildString([desc(visits), source]);
However, your function is not necessary, since Javascript has a built-in function for concatenating array elements with a delimiter:
var string = someArray.join(',');
You're over complicating things โ JavaScript arrays have a built-in join method:
[ desc( visits ), source ].join( ',' );
EDIT: simpler still: the toString method:
[ desc( visits ), source ].toString();