Answers for "split array in 4"

3

react split array into chunks

const chunkSize = 10;
const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
const groups = arr.map((e, i) => { 
     return i % chunkSize === 0 ? arr.slice(i, i + chunkSize) : null; 
}).filter(e => { return e; });
console.log({arr, groups})
Posted by: Guest on January-24-2020
9

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
Posted by: Guest on August-02-2019

Code answers related to "Javascript"

Browse Popular Code Answers by Language