I have multiple arrays in a function that I want to use in another function. How can I return them to use in another function
this.runThisFunctionOnCall = function(){
array1;
array2;
array3;
return ????
}
JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.
You can return only one value in Java. If needed you can return multiple values using array or an object.
We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data.
A function can have more than one return statement, but only ever run one based on a condition.
as an array ;)
this.runThisFunctionOnCall = function(){
var array1 = [11,12,13,14,15];
var array2 = [21,22,23,24,25];
var array3 = [31,32,33,34,35];
return [
array1,
array2,
array3
];
}
call it like:
var test = this.runThisFunctionOnCall();
var a = test[0][0] // is 11
var b = test[1][0] // is 21
var c = test[2][1] // is 32
or an object:
this.runThisFunctionOnCall = function(){
var array1 = [11,12,13,14,15];
var array2 = [21,22,23,24,25];
var array3 = [31,32,33,34,35];
return {
array1: array1,
array2: array2,
array3: array3
};
}
call it like:
var test = this.runThisFunctionOnCall();
var a = test.array1[0] // is 11
var b = test.array2[0] // is 21
var c = test.array3[1] // is 32
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With