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
function with parameters as an array with objects javascript - Stack Overflow
How to pass an array as argument to a function in javascript? - Stack Overflow
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]));You need to make the function accept a parameter. Simply put a suitable name for the parameter in the parentheses of your the function declaration like this:
var func = function(items){
[...]
}
Inside the function, you can access the parameter by simply using it's name. In your case it would be like this:
var func = function(items){
var feeded = items;
}
You can read more about functions in javascript here
Actually you can iterate those with a for Loop
var func = function (){
var feeded = arguments;
console.log(arguments);
for(var i = 0; i < feeded[0].length; i++)
console.log(feeded[0][i].stat);
}
var itemArray = [{"stat":49,"amount":156},{"stat":40,"amount":108},{"stat":5,"amount":207},{"stat":7,"amount":311}] //just an exsample
var answare = func(itemArray)
You might just have forgot to access arguments[0] instead of arguments
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();