我如何从全局函数声明一个数组?

我想从全局函数声明一个数组,以便我的其他函数也可以使用它,但我不知道如何,因为我使用了一个csvtojson转换器,使整个事情很长,并想知道如果这是申报还是不申请?

JS:

//require the csvtojson converter class var Converter = require("csvtojson").Converter; // create a new converter object var converter = new Converter({}); var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/myproject'; // call the fromFile function which takes in the path to your // csv file as well as a callback function var JSarray = converter.fromFile("./NTA-SAM-Inventory-List-Security- Management-New_2017.csv",function(err, result, callback){ if(err){ console.log("An Error Has Occured"); console.log(err); } // the result of the conversion console.log(result); result.toArray(function(err,doc){ if(err) throw err; console.log('ohhhhh'); return callback(null, doc); } }); 

var array=[JSarray(function(err,doc)]这是我如何声明数组。我的数组是doc,所以我可以返回callback,但我应该如何得到数组考虑我有converter.fromFile("./NTA-SAM-Inventory-List-Security- Management-New_2017.csv"这个太长了,所以在声明数组的时候我省略了,或者我做错了?谢谢。

更新只是想澄清,如果我做得对。

 var JSarray = converter.fromFile("./NTA-SAM-Inventory-List-Security-M anagement-New_2017.csv",function(err, result, callback){ // if an error has occured then handle it if(err){ console.log("An Error Has Occured"); console.log(err); } // the result of the conversion console.log(result); result.toArray(function(err,doc){ if(err) throw err; console.log('ohhhhh'); return callback(null, doc); var myArray= doc; GLOBAL.GlobalMyArray = myArray; }); }); 

这是否正确地遵循你的答案在全球范围内宣布?

您可以通过将函数分配给窗口对象来在函数内设置全局数组。

 function myFunction() { var myArray = new Array(); window.GlobalMyArray = myArray; } 

一旦设置,您可以从任何地方使用GlobalMyArray

使用Node.Js你可以使用全局

 function myFunction() { var myArray = new Array(); GLOBAL.GlobalMyArray = myArray; } 

现在可以使用GLOBAL.GlobalMyArray全局访问数组

在这种情况下,这样做非常有用:

 GLOBAL.window = GLOBAL 

就像在网页浏览器中一样。

全局variables不被认为是很好的编程实践。

虽然您可以创build一个全局对象模块,并在项目的所有其他模块中引用这些模块,并使用它的公开属性进行播放。

一个简单的例子就是在node.js里面:

创build模块GlobalArray 。 这个模块将公开它定义的类的单例对象。 这个类的构造函数创build成员variables。 这个类现在使用AddGet等方法暴露成员variables。 这个类的实例是从模块中导出的。

 // File: GlobalArray.js class GlobalArray { constructor(){ this.array = []; } Add(item) { this.array.push(item); } Get(){ return this.array; } } let globalArray = new GlobalArray(); module.exports = globalArray; 

将数据添加到Global Array的类的模块可以创build如下:

 // File: Add.js const globalArray = require("./GlobalArray"); class Add { AddToGlobalArray(){ globalArray.Add("1"); globalArray.Add("2"); globalArray.Add("3"); globalArray.Add("4"); globalArray.Add("5"); } } module.exports = Add; 

可以创build从全局数组打印数据的类的模块,如下所示:

 // File: Print.js const globalArray = require("./GlobalArray"); class Print { PrintGlobalArray(){ let array = globalArray.Get(); for(let i=0; i<array.length; i++){ console.log(array[i] + "\n"); } } } module.exports = Print; 

AddPrint模块都使用语句const globalArray = require("./GlobalArray")来引用全局数组

现在,我们可以通过在index.js中引用它们来使用AddPrint模块来分别使用Global Array来添加和打印数据。

 // File: index.js const Add = require("./Add"); const Print = require("./Print"); // Creating object of Add module to add data to global array let addObject = new Add(); addObject.AddToGlobalArray(); // Creating object of Print module to print data from global array let printObject = new Print(); printObject.PrintGlobalArray(); 

在执行index.js之后 ,它会呈现以下输出:

 >node index.js 1 2 3 4 5