I would like to split a string into parts with a fixed length (N, for example). Of course, the last piece can be shorter if the original string length is not a multiple of N.
I need the fastest way to do this, but also the easiest to write. So far I have done it like this:
var a = 'aaaabbbbccccee';
var b = [];
for(var i = 4; i < a.length; i += 4){
b.push(a.slice(i-4, i));
}
b.push(a.slice(a.length - (4 - a.length % 4)));
I think there should be a better way to do what I want. But I do not want additional modules or libraries, just JavaScript, if possible.
Before asking, I saw some solutions to solve this problem using other languages, but they are not designed with JavaScript in mind.
source
share