javascript how to split array into subarrays javascript
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 array.
let randomArray = [3, 5, 1, 5, 7,];
// Create an empty array.
let arrayOfArrays = [];
function splitArray( array ) {
while (array.length > 0) {
let arrayElement = array.splice(0,1);
arrayOfArrays.push(arrayElement);
}
return arrayOfArrays;
}
// Call the function while passing in an array of your choice.
splitArray(randomArray)
// => [ [ 3 ], [ 5 ], [ 1 ], [ 5 ], [ 7 ] ]
Also in JavaScript:
- angular limit string length
- count occurrences of character in string javascript
- js class static
- js find duplicates in array
- referenceerror document is not defined node js
- The expression cannot be a selector (occur after a dot)
- socket emit to specific room using nodejs socket.io
- curl post file json
- javascript multiply array with scalar
- killall node windows
- get the current url javascript
- jquery on click return link url
- json url data is not showing in console using jquery
- mm dd yyyy how to get date in this format in javascript
- javascript clone array of objects
- js how to to attach an event handler only once
- nodejs get file size
- js remove readonly attribute
- javascript every method
- javascript get current month
- nativescript angular back button event
- check if not checked vanila js
- what is @ atsign in first of file path nodejs
- javscript send ajax request only if submit is valid