Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 :: node js try catch 
Javascript :: getstaticpaths with redux 
Javascript :: how to move a channel to a category discord js 
Javascript :: js get environment variable 
Javascript :: download image from url javascript 
Javascript :: express session destroy 
Javascript :: hide / show jquery 
Javascript :: javascript create cookie 
Javascript :: jquery toggle class 
Javascript :: javascript reverse string without reverse method 
Javascript :: javascript grab only even array index 
Javascript :: como saber la resolucion de una ventana con javascript 
Javascript :: scrool to top jquerry 
Javascript :: scroll to bottom of an element javascript 
Javascript :: javascript get random string char 
Javascript :: node.js http request ip address 
Javascript :: newtonsoft json object to json string 
Javascript :: javascript sleep 
Javascript :: At line:1 char:1 + nodemon server.js 
Javascript :: how to get orientation in js 
Javascript :: js detect scroll 
Javascript :: set value input date javascript 
Javascript :: sh: 1: nodemon: not found heroku 
Javascript :: how to use hover functionality using Jquery 
Javascript :: get all input values by class jquery 
Javascript :: rn push notification No task registered for key ReactNativeFirebaseMessagingHeadlessTask 
Javascript :: flutter jsonDecode UTF8 
Javascript :: ajax each function 
Javascript :: vscode file cannot be loaded because running scripts is disabled on this system 
Javascript :: get window resolution javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =