title: iterate an array with conditions such that certain elements can be revisited multiple time
body: <p>For the input below:</p>
  <pre><code>{
      &quot;vals&quot;: [
        {
          &quot;month&quot;: &quot;Jan 2022&quot;,
          &quot;value&quot;: 0,
          &quot;amount&quot;: -200,
          &quot;date&quot;: &quot;01-02-2022&quot;
        },
        {
          &quot;month&quot;: &quot;Feb 2022&quot;,
          &quot;value&quot;: 0,
          &quot;amount&quot;: -200,
          &quot;date&quot;: &quot;28-02-2022&quot;
        }
      ],
      &quot;items&quot;: [
          {
              &quot;date&quot;: &quot;01-02-2022&quot;,
              &quot;amount&quot;: -200
          },
          {
              &quot;date&quot;: &quot;04-02-2022&quot;,
              &quot;amount&quot;: 100
          },
          {
              &quot;date&quot;: &quot;28-02-2022&quot;,
              &quot;amount&quot;: -200
          },
          {
              &quot;date&quot;: &quot;10-03-2022&quot;
              &quot;amount&quot;: 250
          },
          {
              &quot;date&quot;: &quot;12-03-2022&quot;
              &quot;amount&quot;: 50
          }
      ]   
  }
  </code></pre>
  <p>I want the below output</p>
  <pre><code>{
      &quot;vals&quot;: [
        {
          &quot;month&quot;: &quot;Jan 2022&quot;,
          &quot;value&quot;: 37
        },
        {
          &quot;month&quot;: &quot;Feb 2022&quot;,
          &quot;value&quot;: 12
        }
      ]
  }
  </code></pre>
  <p>for month &quot;Jan 2022&quot; of vals, we have debit amount on 01-02-2022. So need go through the items arrays from 01-02-2022 till the total sum of amount matches with vals &quot;Jan 2022&quot; amount value and we need to the no of days between i.e. 01-02-2022 and 10-03-2022</p>
  <p>for month &quot;Feb 2022&quot; instead of going through items array again from first we should continue where we left for Jan 2022.</p>
  <pre><code>    {
          &quot;date&quot;: &quot;10-03-2022&quot;
          &quot;amount&quot;: 250
      }
  </code></pre>
  <p>of this item 100 is considered for Jan 2022 and 150 is considered for Feb 2022.</p>
  <p>so I need to loop items array such that I should continue where I left for an element of vals array.</p>
  <pre><code>    {
          &quot;date&quot;: &quot;10-03-2022&quot;
          &quot;amount&quot;: 250
      }
  </code></pre>
  <p>this element of items array should be considered twice once for Jan 2022 and second for Feb 2022</p>
  <p>But</p>
  <pre><code>    {
          &quot;date&quot;: &quot;04-02-2022&quot;
          &quot;amount&quot;: 100
      }
  </code></pre>
  <p>should considered only once as the 100 will not completely serve Jan 2022 amount i.e. -200</p>
  <p>I hope I made it clear.</p>