I have an array that contains dialogue strings of varying length.
I need to split elements that have more than 200 characters into two separate elements. I would need to do the split so that no words are cut in half (but instead split based on whitespace).
Edit: This question is different from simply splitting a string based on white space because it involves inserting and removing elements in the array in the right locations.
Example:
[
"Hello there I'm shorter than 200 characters so do nothing with me",
"I'm longer than 200 characters though so I should be split into two consecutive elements so that my first half stays where I am currently located in the array and my second half goes to the very next position in the array.",
"I'm shorter than 200 so I just stay here as the last element."
]
Desired result:
[
"Hello there I'm shorter than 200 characters so do nothing with me",
"I'm longer than 200 characters though so I should be split into two consecutive elements so that my first",
"half stays where I am currently located in the array and my second half goes to the very next position in the array.",
"I'm shorter than 200 so I just stay here as the last element."
]
Failed attempt (I have no idea really):
const myArray = [
"Hello there I'm shorter than 200 characters so do nothing with me",
"I'm longer than 200 characters though so I should be split into two consecutive elements so that my first half stays where I am currently located in the array and my second half goes to the very next position in the array.",
"I'm shorter than 200 so I just stay here as the last element."
]
myArray.forEach((element) => {
if (element.length > 200){
let split = element.substring(0,element.length/2);
myArray.push(split);
}
});