Combining Strings In An Array Upto Certain Length
var chars = 100; var s = [ 'when an unknown printer took a galley of type and scrambled it to make a type specimen book', //contains 91 chars 'essentially unchanged. It was popul
Solution 1:
One way of doing it by parsing the array only once but using another array for the result:
var chars = 100;
var s = [
"when an unknown printer took a galley of type and scrambled it to make a type specimen book",
"essentially unchanged. It was popularised in the 1960s with the release",
"unchanged essentially. popularised It was in the 1960s with the release", //contains 71 chars
"It is a long established", //contains 24 chars
"search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years", //contains 121 chars
"injected humour and the like" //contains 28 chars
],
out = [],
tmp;
s.forEach((str, index) => {
tmp = tmp ? tmp + '\n' + str : str;
if (tmp.length > chars || index == s.length - 1) {
out.push(tmp);
tmp = null;
}
});
console.log(out.join('\n\n'));
Post a Comment for "Combining Strings In An Array Upto Certain Length"