title: Javascript : Store JSON data in array
body: <p>I have JSON like this. </p>
  
  <pre><code>[{
      "address": "A-D-1",
      "batch": [{
          "batch_number": "B-123",
          "cost": [{
              "cost": "C1"
          }]
      }]
  },
  {
      "address": "A-85-1",
      "batch": [{
          "batch_number": "B-6562",
          "cost": [{
              "cost": "C16464"
          }]
      }]
  },
  {
      "address": "A-522-1",
      "batch": [{
          "batch_number": "B-4511",
          "cost": [{
              "cost": "C8745"
          }]
      }]
  }]
  </code></pre>
  
  <p>I would like to store my JSON data to array.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');
  
  for (let i = 0; i &lt; data.length; i++) {
  
  if (data[i].batch !== undefined &amp;&amp; data[i].batch !== null &amp;&amp; data[i].batch.length !== undefined &amp;&amp; data[i].batch.length &gt; 0) {
    let batchLength = data[i].batch.length
  
    let newObject = {}
    let newArray = []
  
    for (let j = 0; j &lt; batchLength; j++) {
      if (data[i].batch !== undefined &amp;&amp; data[i].batch[j].cost !== null &amp;&amp; data[i].batch[j].cost.length !== undefined &amp;&amp; data[i].batch[j].cost.length &gt; 0) {
        let costLength = data[i].batch[j].cost.length
        for (let k = 0; k &lt; costLength; k++) {
  
          newObject.location = data[i].address
          newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''
  
          newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
          newArray.push(newObject)
        }
      }
    }
  }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have stored JSON in <code>data</code> variable.</p>
  
  <p>I have tried below code. but I always get last index as repeatative.</p>
  
  <p>Expected Output : </p>
  
  <pre><code>[
     {
        "address":"A-D-1",
        "batch":{
           "batch_number":"B-123"
        },
        "cost":"C1"
     },
     {
        "address":"A-85-1",
        "batch":{
           "batch_number":"B-6562"
        },
        "cost":"C16464"
     },
     {
        "address":"A-522-1",
        "batch":{
           "batch_number":"B-4511"
        },
        "cost":"C8745"
     }
  ]
  </code></pre>
  
  <p>Any help would be great.</p>
  
  <p>Thank You.</p>