Javascript 如何从Javascript中的函数返回多个数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5760058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return multiple arrays from a function in Javascript?
提问by Asim Zaidi
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 ????
}
回答by Caspar Kleijne
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
回答by Marc Bouvier
Simply put your arrays into an array and return it I guess.
只需将您的数组放入一个数组中并返回它我猜。
回答by Vinay
I would suggest making an array of arrays. In other words, a multidimensional array. That way, you can reference all arrays outside of the function within that one returned array. To learn more on how to do this, this link is quite useful: http://sharkysoft.com/tutorials/jsa/content/019.html
我建议制作一个数组数组。换句话说,一个多维数组。这样,您可以在返回的数组中引用函数之外的所有数组。要了解有关如何执行此操作的更多信息,此链接非常有用:http: //sharkysoft.com/tutorials/jsa/content/019.html