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 :: join text in js 
Javascript :: close tab using jquery 
Javascript :: how to change a string to number in javascript 
Javascript :: javascript format number with K M 
Javascript :: get parent id javascript 
Javascript :: random integer in nodejs 
Javascript :: use import in node 
Javascript :: js get input from user 
Javascript :: d3.json() function 
Javascript :: examples of toastr in jquery 
Javascript :: scrollview refresh 
Javascript :: show ajax error wordpress 
Javascript :: how to loop through date range in javascript 
Javascript :: javascript innerwidth 
Javascript :: draw line in javascript 
Javascript :: generate random whole number between 2 javascript 
Javascript :: remove specific property from json object javascript 
Javascript :: json parse string 
Javascript :: decompile electron app 
Javascript :: change checkbox jquery alert 
Javascript :: extract words from string js 
Javascript :: hide a div when user clicks outside of it 
Javascript :: how could you implement javascript into java 
Javascript :: getting href value in jquery 
Javascript :: alert with sound javascript 
Javascript :: nestjs change httpcode inside function 
Javascript :: jquery version how 
Javascript :: start angular app server 
Javascript :: javascript convert number to string with 2 decimal places 
Javascript :: javascript combine dictionaries 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =