js split string in chunks code example
Example 1: javascript split array into chuncks of
function splitArrayIntoChunksOfLen(arr, len) {
var chunks = [], i = 0, n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, i += len));
}
return chunks;
}
var alphabet=['a','b','c','d','e','f'];
var alphabetPairs=splitArrayIntoChunksOfLen(alphabet,2); //split into chunks of two
Example 2: how to split a string into chunks nodejs
function chunkString(str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}