Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to divide array in two parts in js

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; 
 }
 
PREVIOUS NEXT
Tagged: #divide #array #parts #js
ADD COMMENT
Topic
Name
7+4 =