I have a config.json file:
我有一个config.json文件:
{
"test": {
"maths": [
"core",
"core2"
]
}
}
In my index.js, I want to access the object 'test', and iterate through each of the maths elements.
在我的index.js中,我想访问对象'test',并遍历每个数学元素。
I am new to arrays, so still trying to figure out how they work. What is wrong with this approach? (I know it does not work)
我是数组的新手,所以仍然试图弄清楚它们是如何工作的。这种方法有什么问题? (我知道它不起作用)
var CONFIG = require('./config.json')
var fn = CONFIG.test.maths
fn.forEach(function(value){
console.log(value);
});
Also, what is the correct way of doing this?
另外,这样做的正确方法是什么?
4 个解决方案
#1
2
Try changing config.js to
尝试将config.js更改为
module.exports = {
"test": {
"maths": [
"core",
"core2"
]
}
};
As it stands now - your module doesn't export anything.
现在看来 - 你的模块不会导出任何东西。
Update: if you prefer .json - this method won't work as .json can't be exported (but it can be required in node, see How to parse JSON using Node.js?), it works with .js files as modules. And you'll have to change this part too:
更新:如果您更喜欢.json - 此方法将无法工作,因为.json无法导出(但在节点中可能需要它,请参阅如何使用Node.js解析JSON?),它与.js文件一起使用模块。你也必须改变这个部分:
require('./config.js')
Update2: it works as is with index.js
Update2:它与index.js一样工作
var CONFIG = require('./config')
var fn = CONFIG.test.maths
fn.forEach(function(value){
console.log(value);
});
config.json
config.json
{
"test": {
"maths": [
"core",
"core2"
]
}
}
in my node v5.3.0
在我的节点v5.3.0中
#2
0
To fix this rename your config.js file to config.json (For naming convention sake)
要修复此问题,请将config.js文件重命名为config.json(用于命名约定)
Change:
更改:
var CONFIG = require('./config')
To:
至:
var CONFIG = require('./config.json')
The reason is that requiring a file requires the exact filename including the extension
原因是需要文件需要包含扩展名的确切文件名
#3
0
3 posible solutions, choose the one that suits you the most:
3个可行的解决方案,选择最适合您的解决方案:
- Change the file extension of
config.js
, toconfig.json
- 将config.js的文件扩展名更改为config.json
-
Read the file instead and parse it as JSON:
var CONFIG = JSON.parse(require('fs').readFile('./config.js'));
请改为读取文件并将其解析为JSON:var CONFIG = JSON.parse(require('fs')。readFile('。/ config.js'));
-
Make the file a module that exports the JSON instead of plain JSON, as Anton suggested
正如Anton所说,使文件成为导出JSON而不是普通JSON的模块
#4
0
You can simply use the JQuery method getJSON to load the Json into Javascript.
您可以简单地使用JQuery方法getJSON将Json加载到Javascript中。
`$.getJSON( "config.json", function( data ) {
console.log(data);
var val = data.test.maths
val.forEach(function (d) {
console.log(d);
})
});`
#1
2
Try changing config.js to
尝试将config.js更改为
module.exports = {
"test": {
"maths": [
"core",
"core2"
]
}
};
As it stands now - your module doesn't export anything.
现在看来 - 你的模块不会导出任何东西。
Update: if you prefer .json - this method won't work as .json can't be exported (but it can be required in node, see How to parse JSON using Node.js?), it works with .js files as modules. And you'll have to change this part too:
更新:如果您更喜欢.json - 此方法将无法工作,因为.json无法导出(但在节点中可能需要它,请参阅如何使用Node.js解析JSON?),它与.js文件一起使用模块。你也必须改变这个部分:
require('./config.js')
Update2: it works as is with index.js
Update2:它与index.js一样工作
var CONFIG = require('./config')
var fn = CONFIG.test.maths
fn.forEach(function(value){
console.log(value);
});
config.json
config.json
{
"test": {
"maths": [
"core",
"core2"
]
}
}
in my node v5.3.0
在我的节点v5.3.0中
#2
0
To fix this rename your config.js file to config.json (For naming convention sake)
要修复此问题,请将config.js文件重命名为config.json(用于命名约定)
Change:
更改:
var CONFIG = require('./config')
To:
至:
var CONFIG = require('./config.json')
The reason is that requiring a file requires the exact filename including the extension
原因是需要文件需要包含扩展名的确切文件名
#3
0
3 posible solutions, choose the one that suits you the most:
3个可行的解决方案,选择最适合您的解决方案:
- Change the file extension of
config.js
, toconfig.json
- 将config.js的文件扩展名更改为config.json
-
Read the file instead and parse it as JSON:
var CONFIG = JSON.parse(require('fs').readFile('./config.js'));
请改为读取文件并将其解析为JSON:var CONFIG = JSON.parse(require('fs')。readFile('。/ config.js'));
-
Make the file a module that exports the JSON instead of plain JSON, as Anton suggested
正如Anton所说,使文件成为导出JSON而不是普通JSON的模块
#4
0
You can simply use the JQuery method getJSON to load the Json into Javascript.
您可以简单地使用JQuery方法getJSON将Json加载到Javascript中。
`$.getJSON( "config.json", function( data ) {
console.log(data);
var val = data.test.maths
val.forEach(function (d) {
console.log(d);
})
});`