0

I have a JSON file called database.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;

{
  "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.

getfugu
  • 140
  • 1
  • 5
Haydar Öztürk
  • 122
  • 1
  • 13

1 Answers1

1
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

Haydar Öztürk
  • 122
  • 1
  • 13
  • 1
    Glad you found the answer. However, the reason you didn't get the answer is more due to the fact that only 12 minutes have passed, rather than anything else. We do get a lot of badly phrased questions here, this was not one of them. – Amadan Jul 10 '18 at 03:31
  • I saw your answer right after I, thank you for that, I meant in my searches because I couldn't find anything like this with phrasing like this. Thank you very much for trying. If I would phrase it better I would've found it before doing a lot of search on it and spend a lot of time. I think I need to learn mode English :) – Haydar Öztürk Jul 10 '18 at 03:34
  • 1
    It's not English, but rather the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) phenomenon, common in new programmers. "update after splice" assumes you need to use `splice` - but you don't. What you want is to "delete an attribute from an object in JavaScript"; you only think you need `splice`, and this wrong preconception leads you to a wrong trail. – Amadan Jul 10 '18 at 03:37
  • wow, you are right. I am gonna work on that as changing the way I think, thank you. Do you recommend any book or similar things like this I can learn from? I am learning to code for a long time but this is the first time I am seriously loving it, so if I can improve my thinking style it would help more than anything because all the resources out there, I need to be able to focus on problem solving with different angles or does it come as I learn? – Haydar Öztürk Jul 10 '18 at 03:42
  • 1
    I'd say read lots of code, browse Stack Overflow, and as you have time, read through everything [MDN has on JavaScript](https://developer.mozilla.org/bm/docs/Web/JavaScript), with JavaScript Guide being the first, and the entirety of Reference section when you can; meanwhile, be sure you are also working on projects so it's not all theoretical. – Amadan Jul 10 '18 at 03:45