title: How to add item in every N items of array in Javascript
body: <p>In the initial stage, i have an object array:</p>
  
  <pre><code>[
   { type: 'all' },
   { type: 'all' },
   { type: 'all' },
   { type: 'fruit'},
   { type: 'all' },
   { type: 'all' },
   { type: 'fruit' },
   { type: 'all' },
   { type: 'all' },
   { type: 'fruit' },
   { type: 'fruit' }
  ]
  </code></pre>
  
  <p>I want to insert :</p>
  
  <p>1) object: {type: 'toys'} in every 5 items with type: all</p>
  
  <p>2) object: {type: 'clothes'} in every 2 items with type: 'fruit'</p>
  
  <p><strong>Expected Result:</strong></p>
  
  <pre><code>[
   { type: 'all' },
   { type: 'all' },
   { type: 'all' },
   { type: 'fruit'},
   { type: 'all' },
   { type: 'all' },
   { type: 'toys' },
   { type: 'fruit' },
   { type: 'clothes' },
   { type: 'all' },
   { type: 'all' },
   { type: 'fruit' },
   { type: 'fruit' },
   { type: 'clothes' }
  ]
  </code></pre>
  
  <p>How can i implement this function ? I tried to forEach to add the items, but after pushing an item, the array_demo length is changed and hard to measure which is the next original item of the list.</p>
  
  <pre><code>array_demo.forEach((item, index) =&gt; {
    array_demo.push({type: 'demo'});
    console.warn(index);
  });
  </code></pre>