javascript 如何在节点js中返回数组数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14483767/
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 return array of arrays in node js?
提问by open source guy
I have module like this in node js
我在 node js 中有这样的模块
var types = function (){
var typeList= new Array();
typeList[0] = "varchar";
var numericDTs= new Array();
numericDTs[0] = "tinyint";
var binaryDTs= new Array();
binaryDTs[0] = "tinyblob";
var data = array();
data[0] = typeList;
data[1] = numericDTs;
data[2] = binaryDTs;
return data;
}
module.exports = {
types: types,
}
i am calling this module like this
我这样称呼这个模块
var types = require("./include/types");
console.log(types.types());
i got error like this 500 ReferenceError: array is not definedno error if i return only types or typeList or binaryDTs. How return array of arrays in node js?
我得到了这样的错误 500 ReferenceError: array is not defined如果我只返回类型或 typeList 或 binaryDTs,则没有错误。如何在节点js中返回数组数组?
回答by Amberlamps
Your error is here:
你的错误在这里:
var data = array();
Write the following instead:
改为编写以下内容:
var date = [];
Actually replace every new Array()
with []
.
其实每一个替换new Array()
用[]
。
Instead of
代替
var typeList= new Array();
typeList[0] = "varchar";
write var typeList = [ "varchar" ];
and so on.
写var typeList = [ "varchar" ];
等等。
EDIT:
编辑:
Actually your whole function can be reduced to:
实际上,您的整个功能可以简化为:
var types = function() {
return [ [ "varchar" ], [ "tinyint" ], [ "tinyblob" ] ];
};
回答by John Dvorak
Expanding on the other answer,
扩展另一个答案,
assuming you don't use the function anywhere else, you could just write
假设您不在其他任何地方使用该函数,您可以只写
module.exports = {
types: function(){
return [["varchar"], ["tinyint"], ["tinyblob"]];
}
}