[英]Updating JSON file after splice
I have a JSON file called database.json. 我有一个名为database.json的JSON文件。 I manage to update, add and manipulate it but couldn't manage to delete from it in a safe way.
我设法更新,添加和操作它,但是无法以安全的方式删除它。 Here is the summary of my problem in code;
这是我在代码中遇到的问题的摘要;
database.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);
Where I commented This, removes is and stores as removed, I can't figure out, how am I gonna update it that onto the file and just delete the key-value pair and rest will be there. 在我对此进行评论的地方,remove是并存储已删除的内容,我不知道该如何将其更新到文件上,然后删除键值对,然后剩下的就在那里。
delete questionsObjects["1"];
The answer was simple just adding this instead of splice works perfect and it updates the database as well. 答案很简单,只需添加它而不是拼接就可以了,并且它也会更新数据库。 It is I guess all about asking the right question and the way I asked wasn't answered because it's in wrong format.
我想这全是关于提出正确的问题,而我的问题却没有得到回答,因为格式不正确。
Where I find the solution after a long search before and some search after this post 经过长时间的搜索和这篇文章之后的一些搜索后,我在哪里找到解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.