All Array Object into n Group?
Sometimes, we face a very common but very confusing situation where we have to think more deeply into it.

letâ€™s say we have 2000 objects in an Array. Now we have to distribute all objects into n groups where n is integer digit. letâ€™s say, 8 groups.

var allObjects = [{â€œidâ€:1}, {â€œidâ€:2}, {â€œidâ€:3}, {â€œidâ€:4}, {â€œidâ€:5}, â€¦â€¦â€¦â€¦,{â€œidâ€:2000}];

Now We need to group into 8 groups.

var grouped = [[{â€œidâ€:1}, {â€œidâ€:2}, {â€œidâ€:3}], [{â€œidâ€:1}, {â€œidâ€:2}, {â€œidâ€:3}].....]

Let's get dirty hands into code. 😀

function createChunked(arr, chunkSize) {
var onjects = [],
i = 0,
lmt = chunkSize;

for (; i < arr.length; i += chunkSize) {

if (lmt == (chunkSize * 8)) {
lmt = lmt + arr.length - (chunkSize * 8);
}
if (lmt <= arr.length) {
var tmpArray = arr.slice(i, lmt);
onjects.push(tmpArray);
lmt = lmt + chunkSize;
}
}
return onjects;
}

var totalObj = allObjects.length;
var count = parseInt(totalObj / 8); // n=8
var grouped = createChunked(allObjects, count);
console.log(grouped);    

Happy Codingâ€¦!!! 🙂