我有一个名为database.json的JSON文件。我设法更新,添加和操作它,但是无法以安全的方式删除它。这是我的代码问题摘要;
database.json;
{
"faqs": {
"questions": {
"1": "Question is deleted",
"2": "b",
"3": "c"
},
"answers": {
"1": "aa",
"2": "bb",
"3": "cc"
}
}
...
}
const fs = require('fs');
const ReadDatabase = fs.readFileSync('database.json');
const ReadData = JSON.parse(ReadDatabase);
let questionsObjects = ReadData.faqs.questions;
let questionObjectKeys = Object.keys(ReadData.faqs.questions)
let removed = questionObjectKeys.splice(0,1); //This
let editedDataBase = JSON.stringify(ReadData, null, 2);
fs.writeFileSync('database.json', editedDataBase);
在我对此进行评论的地方,“ remove is”并存储为“ remove”,我不知道该如何将其更新到文件上,然后删除键值对,剩下的就在那里。
答案 0 :(得分:1)
delete questionsObjects["1"];
答案很简单,只需添加它而不是拼接就可以了,并且它也会更新数据库。我想这一切都是关于提出正确的问题,而我的问题却没有得到回答,因为格式不正确。
Where I find the solution after a long search before and some search after this post