Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript array split chunk

const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);

// Examples
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3);     // [[1, 2, 3], [4, 5, 6], [7, 8]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 4);     // [[1, 2, 3, 4], [5, 6, 7, 8]]
Comment

split array into chunks javascript

Array.prototype.chunk = function(size) {
    let result = [];
    
    while(this.length) {
        result.push(this.splice(0, size));
    }
        
    return result;
}

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(arr.chunk(2));
Comment

split array into chunks javascript

const splitArray=(arr, chunk)=>{
    const elementInEachSubArray = Math.floor(arr.length / chunk)
    const remainingElement = arr.length - (elementInEachSubArray * chunk)
    let splitArray = Array.from({length: chunk}, ()=>[])
    splitArray = splitArray.map(
        (array, i)=>{
            return arr.slice(i*elementInEachSubArray, elementInEachSubArray * (i + 1))
}
    ).map((array, i)=>[...array, arr[arr.length - remainingElement + i]].filter(Boolean))
    console.log(splitArray)
    
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: list of higher-order functions javascript 
Javascript :: react webpack.config.js example 
Javascript :: variable for every user discord.js 
Javascript :: Using Regular Expressions (regex) to Print JavaScript Number Format with Commas 
Javascript :: select field in react native 
Javascript :: count javascript 
Javascript :: is jwt token expired 
Javascript :: search datatable vuetify 
Javascript :: react native float upto 2 digits 
Javascript :: leap year function javascript 
Javascript :: mui date picker width 
Javascript :: webpack-bundle-analyzer react 
Javascript :: javascript array delete first element 
Javascript :: hide div after 5 seconds vue js 
Javascript :: getfullyear javascript 
Javascript :: javascript switch assignment 
Javascript :: vue toggle boolean on click 
Javascript :: add array to array js 
Javascript :: remove element from array by name javascript 
Javascript :: mongoBD increment 
Javascript :: why does my form reload the page? html js 
Javascript :: jquery countdown timer 
Javascript :: Disabale Back History On Browsers JavaScript Jquery 
Javascript :: dynamically change meta tags javascript 
Javascript :: mongodb add key value to all documents 
Javascript :: fuse.js 
Javascript :: apps script toast 
Javascript :: stop() in jquery 
Javascript :: delete node modules 
Javascript :: npm config proxy 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =