In the initial stage, i have an object array:
[
{ type: 'all' },
{ type: 'all' },
{ type: 'all' },
{ type: 'fruit'},
{ type: 'all' },
{ type: 'all' },
{ type: 'fruit' },
{ type: 'all' },
{ type: 'all' },
{ type: 'fruit' },
{ type: 'fruit' }
]
I want to insert :
1) object: {type: 'toys'} in every 5 items with type: all
2) object: {type: 'clothes'} in every 2 items with type: 'fruit'
Expected Result:
[
{ 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' }
]
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.
array_demo.forEach((item, index) => {
array_demo.push({type: 'demo'});
console.warn(index);
});