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 :: find word in javascript string 
Javascript :: dotnet core ajax post of an ojject site:stackoverflow.com 
Javascript :: settimestamp discord.js 
Javascript :: eslint no-param-reassign 
Javascript :: sort array with objects 
Javascript :: eslint ignore current line 
Javascript :: effect jquery 
Javascript :: get unique numbers of an array in javascript using for loop 
Javascript :: js get locale 
Javascript :: first non repeating character javascript 
Javascript :: how to pronounce allele 
Javascript :: javascript body element 
Javascript :: javascript function description standards 
Javascript :: scroll to bottom of a div javascript 
Javascript :: javascript get random item from array 
Javascript :: Iterate Odd Numbers With a For Loop 
Javascript :: regex to extract a phone number with country code 
Javascript :: How find a specific character in js 
Javascript :: how to change the staticness of a object in matter.js 
Javascript :: javascript date - 7 days 
Javascript :: bottom shadow in react native 
Javascript :: check if 2 arrays are equal javascript 
Javascript :: sh 1 nodemon not found heroku 
Javascript :: jquery hover 
Javascript :: convert military time to standard time javascript 
Javascript :: jquery watch checkbox change 
Javascript :: js ask before close chrome 
Javascript :: or in js 
Javascript :: cypress type force 
Javascript :: jquery on input 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =