I am working with an array that looks like this:
const testArray = [ 'blah', 'abctesttt', 'atestc', 'testttttt' ]
My goal is to split the string once it reaches a certain character count, say 10 characters. In addition, I want the output to adjust itself to fit within 10 characters. If this concept is unclear, please refer to the expected output below for clarification. Let's assume each item in the array will not exceed 10 characters for illustration purposes.
When the testArray
exceeds 10 characters, I would like the next element to be stored under a new variable perhaps? I am open to alternative methods if this approach is inefficient.
const testArray = [ 'blah', 'abctesttt', 'atestc', 'testttttt' ]
if ((testArray.join('\n')).length) >= 10 {
/* Would it make sense to split the string into parts and store it in a new variable maybe?
console.log((the_splitted_testArray).join('\n')); */
}
Expected result:
"blah
atestc" // Instead of using "abctesttt", it should use "atestc" from the array to avoid exceeding the 10-character limit. If adding "atestc" would surpass the limit, it should check the next element.
"abctesttt" // It cannot include the remaining "testttttt" as it would exceed the character limit set
"testttttt"