title: How to capture iterated json values into a variable using javascript
body: <p>Problem: I would like to store data into a json file with dynamically changing fields.
  Let's say I would like to generate the following <code>invoice.json</code> file:</p>
  <pre><code>{
   &quot;token&quot;: &quot;000&quot;,
   &quot;invoices&quot;:
   [
    {
     &quot;invoiceId&quot;: &quot;0001-0001-0001-0001&quot;,
     &quot;amount&quot;: 45.67
    },
    {
     &quot;invoiceId&quot;: &quot;0002-0002-0002-0002&quot;,
     &quot;amount&quot;: 11.03
    }
   ]
  }
  </code></pre>
  <p>I have the following commands in theory which I don't think is working (I simplified it for better understanding):</p>
  <pre><code>const tokenValue = &quot;000&quot;
  let invoiceIdValue = &quot;&quot;
  let amountValue = &quot;&quot;
  for(let i=0; i&lt;1, i++) {
   let data = {
    &quot;token&quot;: tokenValue,
    &quot;invoiceId&quot;: invoiceIdValue[i] // &lt;- I don't know how to call the 1st index of the field
    &quot;amount&quot;: amountValue[i]
   }
  }
  </code></pre>
  <p>What is the most readable way to write this into the variable then eventually a file?
  Note: I will be using cypress' command <code>cy.writeFile('invoice.json', data)</code> to store the iterated values.</p>