Having some trouble manipulating this array to get it into the desired form.
This is the initial array:
const children = [
{ type: 'span' },
{ type: 'br' },
{ type: 'span' },
{ type: 'br' },
{ type: 'br' },
{ type: 'span' },
{ type: 'br' },
{ type: 'br' },
{ type: 'br' },
{ type: 'span' },
{ type: 'br' },
{ type: 'span' },
]
The goal is to create an array of arrays where consecutive br
values indicate a new subarray. The desired outcome should look like this:
[
[{type: 'span'}, {type: 'br'}, {type: 'span'}],
[{type: 'span'}],
[{type: 'span'}, {type: 'br'}, {type: 'span'}]
]
Please Note: There can be any number of consecutive br
tags in the original array.
Considering whether using reduce
would be a more efficient approach compared to forEach
.