Good day,
Is there a way to make a for loop in NodeJS and copy the same json object but with different 'id' into JSON file.
Lets say that i want to add 100 objects in this format :
[ { "id": 1, "Firstname": "Sven","Secondname": "Petersen","Age": "18" }]
when the server starts.
What would be the best way to make it ? What modules should i use ?
I thought i could do something like that :
for (var i = 0; i < 500; i++) {
fs.readFile("JSON/DB.json", "utf8", function () {
var data = '[{ "id": 1, "Firstname": "Sven", "Secondname": "Petersen", "Age": "18", }]'
var Data = JSON.parse(data);
var id = i;
for (var i = 0; i < Data; i++) {
};
var newData = JSON.stringify(Data);
fs.writeFile("JSON/DB.json", newData, "utf8");
})
};
P.S i'm a newb here
fsis the module you are searching for when you wish to write to file. I bet googling "how to write to file nodejs" would give you a good clue on what to use ;P