I had a problem where i sa feeding an Function with an array with objects inside.
var func = function (){
var feeded = //here is my problem
/*some code/*
}
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)
How do i med the variable feeded the same as itemArray, so i can use the methods for arrays on feeded?
I have tried useing:
var feeded = [].slice.call(arguments)
and
var feeded = arguments
I could not use a for loop and then get the stat and amount inside. (i am not needing to close lines as it is not needed in google script
3 Answers 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
Comments
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
7 Comments
arguments ??func('a', 'b'); would have arguments[0] == 'a' and arguments[1] == 'b'arguments is a key world?/arguments isn't quite an array, but [].slice.call(arguments) will return its contents as an array.arguments.length = 1.Try:
var func = function (){
var feeded = arguments; //is itemArray Array
console.log(feeded)
/*some code*/
}
var itemArray = [{"stat":49,"amount":156},{"stat":40,"amount":108},{"stat":5,"amount":207},{"stat":7,"amount":311}] //just an exsample
var answare = func.apply(null, itemArray)
2 Comments
arguments ??arguments = itemArray[]
var func = function(feeded) { ... }should work for you, given that you are invoking it withfunc(itemArray). Alternately,var func = function() { var feeded = arguments[0]; ... }, if you really, really hate that parameter there. It would be a different matter if you invokedfunc.apply(this, itemArray), but there is no reason to.func(itemArray), you are only getting a single argument in the function. You don't need to accessargumentsunless you want to handle the syntaxfunc(),func(item),func(item, item)... and notfunc(itemArray).