I have this array:
data: [
‘big text 1 here.’,
‘big text 2 here followed by list:’,
'-this is list item;',
'-this is another list item;',
‘big text 3 here followed by list:’,
'-this is list item;',
'-this is another list item;',
'-this is another list item;',
‘big text 4 here.’
],
I want to turn it to something like:
data: [
{
text: ‘big text 1 here.’
list: []
},
{
text: ‘big text 2 here followed by list:’
list: [
'-this is list item;',
'-this is another list item;'
]
},
{
text: ‘big text 3 here followed by list:’
list: [
'-this is list item;',
'-this is another list item;',
'-this is another list item;',
]
},
{
text: ‘big text 4 here.’
list: []
},
],
I am trying to achieve this this way
interface item {
text: string;
list: string[];
};
const newArr: item[] = [];
this.item.content.forEach((x, i) => {
if(x.startsWith('-') && x.endsWith(';')) {
//this is bullet list, need to add that to the previous item...
} else {
newArr.push({ text: x, list: []});
}
});
I am not sure if I am doing it in a right way, because there is no way I can think of how to access previous element in newArr; Any ideas how to do it proper way?
That is the output that I want to have based on that data:
<p>big text 1 here</p>
<p>big text 2 here followed by list:</p>
<ul>
<li>this is list item</li>
<li>this is another list item</li>
</ul>
<p>big text 3 here followed by list</p>
<ul>
<li>this is list item</li>
<li>this is another list item</li>
<li>this is another list item</li>
<p>big text 4 here</p>
‘
and’
?