0

I am trying to either update or remove (and then rewrite) a JSON key in node.js

JSON:

{"users":[{"Discordid":"discid","Username":"user","Password":"pass","School":"schoolname"}, {"Discordid":"discid1","Username":"user1","Password":"pass1","School":"schoolname1"}]}

I want to remove the entire {"Discordid":"discid","Username":"user","Password":"pass","School":"schoolname"} via a for loop so I make use of variable a which equals the number in which the data is I want to delete.

I have tried:

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     for (a = 0; a < Object.keys(magistercreds.users).length; a++) delete magistercreds.users[a]

And other things which all did not work.

2
  • Why Object.keys(magistercreds.users), if it is an array, not object? Why not magistercreds.users.length? Commented Apr 5, 2020 at 17:15
  • And you can use .splice to remove an item, or an empty space will be left Commented Apr 5, 2020 at 17:17

2 Answers 2

1
fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     for (a = 0; a < magistercreds.users.length; a++){
          magistercreds.users.splice( a, 1 );
          a--; // to step back, as we removed an item, and indexes are shifted
     }

But may be you want just update, so you can make it simple:

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     magistercreds.users[68468] = {.....}
Sign up to request clarification or add additional context in comments.

Comments

1

The question isn't clear on whether a key is to be removed or an entire object. Assuming an entire element is to be removed from the array users, the splice method can be used.

First find the index of the element you want to remove using findIndex. Then you can use splice to modify the array in-place.

Sample:

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     // Assuming you want to delete the element which has the Discordid property as "discid"
     var indexOfElement = magistercreds.findIndex(el => el.Discordid === "discid")
     magistercreds.users.splice(indexOfElement, 1) // This will remove 1 element from the index "indexOfElement" 
}

To add on, using Object.keys to iterate over an array is not necessary. The for loop in the original question can be rewritten as:

for (a = 0; a < magistercreds.users.length; a++) delete magistercreds.users[a]

Please edit the question to add more information if this is not what you were looking to achieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.