Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to divide array in chunks

function chunkArray(arr,n){
     var chunkLength = Math.max(arr.length/n ,1);
     var chunks = [];
     for (var i = 0; i < n; i++) {
         if(chunkLength*(i+1)<=arr.length)chunks.push(arr.slice(chunkLength*i, chunkLength*(i+1)));
     }
     return chunks; 
 }
Comment

divide array in chunks

function* generateChunks(array, size) {
    let start = 0;
    while (start < array.length) {
        yield array.slice(start, start + size);
        start += size;
    }
}

function getChunks(array, size) {
    return [...generateChunks(array, size)];
}

console.log(getChunks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 3)) // [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 9 ] ]
Comment

PREVIOUS NEXT
Code Example
Javascript :: manter alguns campos objetos javascript 
Javascript :: Javascript: take every nth Element of Array 
Javascript :: npm function-memoizer 
Javascript :: get page scrolling amount js 
Javascript :: flip image on y axis phaser 
Javascript :: regex tunisian phone number 
Javascript :: how to generate random 6 digit charecter in js for coupon 
Javascript :: phaser rotate around distance 
Javascript :: phaser create animation from texture atlas 
Javascript :: phaser play animation with config.js 
Javascript :: lookbehind alternative regex 
Javascript :: Pretty-Print JSON within Neovim 
Javascript :: when end sound show alert 
Javascript :: react native bootsplash generate splash 
Javascript :: store reference of event listener inside a element 
Javascript :: javascript list all elements in set 
Javascript :: check if value is a string javascript 
Javascript :: what does the ... mean in javascript 
Javascript :: filter 
Javascript :: sorting an array 
Javascript :: string charat 
Javascript :: javascript load content from file 
Javascript :: react create context 
Javascript :: angular set timezone 
Javascript :: create slice redux 
Javascript :: how to convert json to object 
Javascript :: spotify player react 
Javascript :: multiple path names for a same component in react router 
Javascript :: javascript code checker 
Javascript :: the event object 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =