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 :: inheritence in javascript 
Javascript :: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:compile 
Javascript :: angularfire 
Javascript :: nested object 
Javascript :: how to multiply two array in javascript 
Javascript :: express.js routing 
Javascript :: page object 
Javascript :: symfony iterate over entity 
Javascript :: Uncaught TypeError: document.getElementById(...).exitFullscreen is not a function 
Javascript :: limpiar historial angular 
Javascript :: javascript powerpoint 
Javascript :: @typescript-eslint/no-empty-function 
Javascript :: counting number of times a string is in another string 
Javascript :: react native time set state 
Javascript :: extract image in p5.js 
Javascript :: js delete without changing index 
Javascript :: (state.isLoggedIn = function() {return false}) react redux 
Javascript :: get object property dynamically liquid 
Javascript :: Google Web App Script Unknown Parameter Error on Load 
Javascript :: getting json from response using getSync method 
Javascript :: angularjs promise .then() to run sequentially inside a for loop 
Javascript :: Angular Frontend - How do I change a value I got from backend in frontend 
Javascript :: How to query a button with specific text with react native testing library 
Javascript :: wrapping a span tag with an a tag with a href target same as the text of the span 
Javascript :: sort lowest to highest js 
Javascript :: upload node js 
Javascript :: export from json 
Javascript :: set of these properties: in js 
Javascript :: create instance method javascript 
Javascript :: javascript check if a number starts with another number 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =