I am currently working with a JSON file stored separately. Here is the code snippet I am using:
[{"username":"John","id":"1","points":[{"type":"gold","amount":1}]},
{"username":"Mark","id":"2","points":[{"type":"bronze","amount":13}]},
{"username":"Mayou","id":"3","points":[{"type":"silver","amount":10}]}]
let username = "John";
let id = 1;
let amount = 1;
let type = silver;
const configDirectory = path.resolve(process.cwd(), "essentials");
let tableSend = JSON.parse(readFileSync(path.join(configDirectory, '../essentials/tableSend.json')));
const exist = tableSend.filter(item => item.id == id).length > 0;
if (exist == true) {
} else {
const values = {
username: username,
id: id,
points: [{
type: type,
amount: amount
}]
}
tableSend.push(values);
let newData = JSON.stringify(tableSend);
writeFileSync(path.join(configDirectory, '../essentials/tableSend.json'), newData);
}
How can I update existing data or add new data to the JSON? The desired result should look like this:
[{"username":"John","id":"1","points":[{"type":"gold","amount":1},{"type":"silver", "amount":1}]},
{"username":"Mark","id":"2","points":[{"type":"bronze","amount":13}]},
{"username":"Mayou","id":"3","points":[{"type":"silver","amount":10}]}]
Or how do I adjust the counts accordingly?
[{"username":"John","id":"1","points":[{"type":"gold","amount":50}]},
{"username":"Mark","id":"2","points":[{"type":"bronze","amount":13}]},
{"username":"Mayou","id":"3","points":[{"type":"silver","amount":10}]}]