Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to divide equal 3 parts of an array javascript

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const threePartIndex = Math.ceil(list.length / 3);

const thirdPart = list.splice(-threePartIndex);
const secondPart = list.splice(-threePartIndex);
const firstPart = list;     

console.log(firstPart);  // [1, 2, 3]
console.log(secondPart); // [4, 5, 6]
console.log(thirdPart);  // [7, 8, 9]
Comment

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; 
 }
Comment

PREVIOUS NEXT
Code Example
Javascript :: split text and join in js 
Javascript :: ajax post body parameters 
Javascript :: letter javascript regex 
Javascript :: get child routes using parent in angular 
Javascript :: javascript random number in range 
Javascript :: Vuejs v-model when enter pressed 
Javascript :: require express 
Javascript :: discord js fetch user 
Javascript :: discord.js rich embed 
Javascript :: dom click is not a function 
Javascript :: speedtest-net node.js 
Javascript :: jquery get mouse page left and top 
Javascript :: vue js cdn link 
Javascript :: javascript get url parameters 
Javascript :: replace double slash with single slash node.js 
Javascript :: Capitalise a String 
Javascript :: mongoose check if string is objectid 
Javascript :: javascript parse json string 
Javascript :: Sum of odd Fibonacci numbers JS 
Javascript :: how to check if item is in list js 
Javascript :: chartjs disable animation 
Javascript :: jquery each loop 
Javascript :: how get value of json encode in laravel 
Javascript :: javascript get ip 
Javascript :: javascript console log execution time 
Javascript :: delay statement in js 
Javascript :: angular dynamic class 
Javascript :: angular serve 
Javascript :: jquery see if checkbox is checked 
Javascript :: write to console using jQuery 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =