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 :: How to insert divider in react native 
Javascript :: moment get week 
Javascript :: sql json_extract 
Javascript :: router navigatebyurl 
Javascript :: get current date + 1 js 
Javascript :: react native create view dynamically 
Javascript :: jquery refresh image without refreshing page 
Javascript :: get express variable 
Javascript :: jquery add element to array 
Javascript :: get child of child javascript 
Javascript :: Nuxt JS Adding script tag to particular page 
Javascript :: js fizzbuzz 
Javascript :: Parcel, how to fix the `regeneratorRuntime is not defined` error 
Javascript :: vue.js cdn script 
Javascript :: get random element from array js 
Javascript :: try catch in node js 
Javascript :: jquery replace text in button 
Javascript :: how to find the last object in an array 
Javascript :: express get client ip 
Javascript :: js get current function name 
Javascript :: get content of textarea javascript 
Javascript :: xhr 
Javascript :: javascript rtsp player 
Javascript :: convert object to array javascript 
Javascript :: javascript create array from 1 to n 
Javascript :: fetch api map 
Javascript :: search class regex 
Javascript :: angular formData print values 
Javascript :: javascript check if object property exists 
Javascript :: mongoose custom object schema 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =