最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何从另一个javascript文件获取对象键和值

网站源码admin25浏览0评论

如何从另一个javascript文件获取对象键和值

如何从另一个javascript文件获取对象键和值

我正在使用$ npm运行index.js

在index.js文件中,我正在循环并获取以下文件的列表,我们需要从该文件中读取“ testData”,请您帮忙获取数据

/test/fileOne.js
/test/example.js

每个文件都有

/test/fileOne.js

var testData = {
    tags: 'tag1 tag2 tag3',
    setup: 'one_tier'
}
/test/example.js

var testData = {
    tags: 'tag3',
    setup: 'two_tier'
}

我的代码:index.js

let fs = require("fs")
const glob = require("glob");

var getDirectories = function (src, callback) {
    glob(src + '/**/*.js', callback);
};
getDirectories('tests', function (err, res) {
if (err) {
    console.log('Error', err);
} 
else {
    var listOfFiles = res;
    for (let val of listOfFiles){
       ///// HERE we have to get the Tags and setup from each js file////
    }
}

回答如下:

您可以尝试一种解决方案。在index.js中包含另一个JS文件]

所以您的index.js会像

const fileOne= require("./test/fileOne.js")
const example= require("./test/example.js")

console.log(fileOne)
console.log(example)

//........... rest of your code
发布评论

评论列表(0)

  1. 暂无评论