【发布时间】:2021-05-08 06:16:36
【问题描述】:
我有以下 json 文件。
{
"nextId": 5,
"notes": {
"1": "The event loop is how a JavaScript runtime pushes asynchronous callbacks onto the stack once the stack is cleared.",
"2": "Prototypal inheritance is how JavaScript objects delegate behavior.",
"3": "In JavaScript, the value of `this` is determined when a function is called; not when it is defined.",
"4": "A closure is formed when a function retains access to variables in its lexical scope."
}
}
通过使用 fs.readFile,我试图仅显示如下属性。 1:事件循环是 JavaScript 运行时在堆栈被清除后将异步回调推送到堆栈上的方式。 2:原型继承是JavaScript对象委托行为的方式。
但我的代码显示了整个 JSON 文件。我的代码如下:
const fs = require('fs');
const fileName = 'data.json';
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) throw err;
const databases= JSON.parse(data);
//databases.forEach(db=>{
console.log(databases);
//});
//console.log(databases);
});
【问题讨论】:
标签: javascript node.js json object readfile