
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Split Array of items into N Arrays in JavaScript
We are required to write a JavaScript function that splits an Array of numbers into N groups, which must be ordered from larger to smaller groups.
For example, in the below code, split an Array of 12 numbers into 5 Arrays, and the result should be evenly split, from large (group) to small:
const arr = [1,2,3,4,5,6,7,8,9,10,11,12]; const output = [[1,2,3] [4,5,6] [7,8] [9,10] [11,12]];
The function should take in the array as the first argument and the number of partitions as the second argument.
Example
The code for this will be −
const arr = [1,2,3,4,5,6,7,8,9,10,11,12]; const chunkArray = (arr = [], chunkCount) => { const chunks = []; while(arr.length) { const chunkSize = Math.ceil(arr.length / chunkCount−−); const chunk = arr.slice(0, chunkSize); chunks.push(chunk); arr = arr.slice(chunkSize); }; return chunks; }; console.log(chunkArray(arr, 5));
Output
And the output in the console will be −
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ], [ 9, 10 ], [ 11, 12 ] ]
- Related Articles
- Split number into n length array - JavaScript
- Can split array into consecutive subsequences in JavaScript
- Split one-dimensional array into two-dimensional array JavaScript
- Converting array of arrays into an object in JavaScript
- JavaScript Converting array of objects into object of arrays
- Turning a 2D array into a sparse array of arrays in JavaScript
- Combine unique items of an array of arrays while summing values - JavaScript
- Split tuple into groups of n in Python
- Split Array by part base on N count in JavaScript
- How to combine two arrays into an array of objects in JavaScript?
- Splitting array of numbers into two arrays with same average in JavaScript
- Split string into groups - JavaScript
- Split Array into Consecutive Subsequences in C++
- How to add two arrays into a new array in JavaScript?
- How can we make an Array of Objects from n properties of n arrays in JavaScript?

Advertisements