I have a collection of content blocks structured like this:
interface Content{
type: string,
content: string | string[]
}
const content: Content[] = [
{
type: "heading"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "heading"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "list_item"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
]
I am attempting to develop a function that will consolidate these list_item
's into a single list
with an array for the content of each item. Therefore, the result of the function for the given input should be:
[
{
type: "heading"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "list"
content: ["whatever","whatever","whatever"]
},
{
type: "para"
content: "whatever"
},
{
type: "heading"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "para"
content: "whatever"
},
{
type: "list"
content: ["whatever", "whatever", "whatever", "whatever"]
},
{
type: "para"
content: "whatever"
},
]
I have been experimenting with a three-pointer approach, iterating through the array from i=1 to i < length - 1, keeping track of the prev
, curr
, and next
blocks. However, I am struggling with the logic and how to handle different scenarios.
It seems like a relatively straightforward problem for seasoned algorithm designers, so I am seeking some guidance.