Trying to remove an object from a JSON file in JavaScript has proven to be more challenging than expected.
Here is an example of the JSON file structure:
{
"tom cruise": {
"player": "tom cruise",
"team": "buf",
"position": "qb",
"overall": "82",
"OnBlock": true
},
"tim tebow": {
"player": "tim tebow",
"team": "buf",
"position": "qb",
"overall": "82",
"OnBlock": false
}
}
And this is what has been attempted so far:
client.block = require ("./jsons/tradeblock.json")
if (message.content.startsWith('tb!remove')) {
player = message.content.toLowerCase().slice(10)
array = player.toLowerCase().split(" - ")
team = array[0]
position = array[1]
player = array[2]
overall = array[3]
client.block [player] = {
player: player,
team: team,
position: position,
overall: overall,
OnBlock: false
}
fs.writeFile ("./jsons/tradeblock.json", JSON.stringify (client.block, null, 4), err => {
if (err) throw err;
message.channel.send("Removing from block")
});
}
Is there a way to check if the "OnBlock" property is false and delete the entire player from the JSON?