title: How to push object inside an array while writing to a file in Node Js
body: <p>How to push object inside an array while writing to a file in Node Js?
  I currently have the following working code</p>
  
  <pre><code>fs.appendFile("sample.json", JSON.stringify(data, undefined, 2) + ",");
  </code></pre>
  
  <p>where data is an object, like </p>
  
  <pre><code>{
  name:'abc',
  age: 22,
  city: 'LA'
  }
  </code></pre>
  
  <p>Upon appending, which results the following</p>
  
  <pre><code>{
  name:'abc',
  age: 22,
  city: 'LA'
  },
  {
  name:'def',
  age: 25,
  city: 'MI'
  },
  </code></pre>
  
  <p>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</p>
  
  <p>Any leads would be appreciated</p>