How to push object inside an array while writing to a file in Node Js? I currently have the following working code
fs.appendFile("sample.json", JSON.stringify(data, undefined, 2) + ",");
where data is an object, like
{
name:'abc',
age: 22,
city: 'LA'
}
Upon appending, which results the following
{
name:'abc',
age: 22,
city: 'LA'
},
{
name:'def',
age: 25,
city: 'MI'
},
I see 2 problems here. 1. The trailing comma at the end 2. The inability to form an array and push the object into it
Any leads would be appreciated