split an array js 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);
Example 2: string split javascript
var myString = "An,array,in,a,string,separated,by,a,comma";
var myArray = myString.split(",");
Example 3: javascript split
var arr = foo.split('');
console.log(arr);
Example 4: javascript how to split array into subarrays javascript
let randomArray = [3, 5, 1, 5, 7,];
let arrayOfArrays = [];
function splitArray( array ) {
while (array.length > 0) {
let arrayElement = array.splice(0,1);
arrayOfArrays.push(arrayElement);
}
return arrayOfArrays;
}
splitArray(randomArray)
Example 5: split array javascript
arr.slice(start, end);
Example 6: javascript split
var arr = ['hey', 'nay', 'wey'];
var let = arr.split(' ');
console.log(let);