My array consists of sentences like the ones below:
[
"First sentence ",
"Second sentence",
"Third sentence {variable}",
"Fourth sentence {variable} with other data {variable2}",
"Fiftth sentence {variable} with additional data {variable2}",
"Sixth sentence"
]
I aim to split the items in the array based on specific conditions.
If a line contains a variable within curly brackets and has content following it, that line should be divided into two lines. Otherwise, it can remain as is.
The desired output is demonstrated below:
[
"First sentence ",
"Second sentence",
"Third sentence {variable}",
"Fourth sentence {variable}",
"with other data {variable2}",
"Fifth sentence {variable}",
"with additional data {variable2}",
"Sixth sentence"
]
In my attempt at solving this, I have created the following method:
convertLogicsArray(sentenceArray: string[]): string[] {
const newSentenceArray: string[] = []
for (const sentence of sentenceArray) {
// Split the sentence into two parts at the variable.
const parts = sentence.split('{', 1)
// If there is text after the variable, make the sentence two lines.
if (parts.length === 2) {
newSentenceArray.push(parts[0])
newSentenceArray.push(parts[1])
}
// Otherwise, leave the sentence as one line.
else {
newSentenceArray.push(logic)
}
}
return newSentenceArray
}
However, this approach does not achieve the exact outcome I desire.
How can I manipulate the array to match the structure outlined in the question when given the original array?