Split object into chunks

2024/2/27 8:48:45

I have an object:

var object = { '232510350': 672,
'232510352': 672,
'232510353': 672,
'232510356': 672,
'232510358': 672,
'232510359': 672,
'232510360': 672 }

And I want to split it into chunks of a given size like this:

var chunks = [
{'232510350': 672,
'232510352': 672,
'232510353': 672},
{'232510356': 672,
'232510358': 672,
'232510359': 672},
{'232510360': 672}]

I'm kinda stuck here since I can't use slice for this

Here's how I did it but it's ugly. I just wanted to see how to do it the right way

var temp = {};
var chunks = [];
var chunkSize = 3;
var amount = chunkSize;for (var i = 0; i < Object.keys(object).length; i++) {if (i == chunkSize) {chunks.push(temp);chunkSize += amount;temp = {};}temp[ Object.keys(object)[i] ] = object[ Object.keys(object)[i] ];if (i == Object.keys(object).length - 1) {chunks.push(temp);}

So, here it is:

var object = { '232510350': 672,
'232510352': 672,
'232510353': 672,
'232510356': 672,
'232510358': 672,
'232510359': 672,
'232510360': 672 }
var values = Object.values(object);
var final = [];
var counter = 0;
var portion = {};
for (var key in object) {
if (counter !== 0 && counter % 3 === 0) {
portion = {};
portion[key] = values[counter];


Related Q&A

Get canvas width and height using javascript

Im currently trying to receive the width and height of a canvas. The canvas is using a style parameter as you can see in the following code:<canvas id="ctx" style="height: calc(100vh …

Function names defined as parameters to a function call arent hoisted. Why not?

Consider the following code.<!DOCTYPE html> <script>console.log(a);function a() {} </script>Notice that a is seemingly accessed before it is defined. The console output is: (jsfiddle)…

simulate backspace key on text input field

Can someone provide a good example to simulate the backspace key on a <INPUT> text field?I need this for a virtual keyboard done in HTML5 and it only needs to work in Webkit browsers.Notes:creat…

Best approach to avoid javascripts this mistakes

In some cases, the this keyword may not refer to the object I expect it to. (recent example: in an key event, in my XBL)Whats the best approach to avoid this kind of mistake?For now, Im using always t…

Javascript to Jquery, add text in input onclick

How can I change this javascript code to JQuery.<script type="text/javascript">function addTextTag(text){document.getElementById(text_tag_input).value += text;} </script>W…

Node module.exports returns undefined

I am having an issue with Node.js and module.exports. I understand that module.exports is a call to return an object, that object having whatever properties it is assigned.If I have a file structure li…

Greasemonkey script to reload the page every minute

How to reload a page every 60 seconds?My attempt: setTimeout (location.reload, 1 * 60 * 60);Im not sure what those numbers mean, or how to adapt them to reload after 60 seconds only.

In a While loop setTimeout not working

I am 11 and just started programming. I was trying to use a while loop and I want it to display the answers in blocks instead of one huge chunk. So i tried to use a setTimeout but it would just display…

Pass a message from Chrome Extension to Webpage

I need to pass a message (raise an event) in a Chrome extension, and have JavaScript on a web page react to it.In content_script.js of the extension, there should be a function likeraiseXYZevent(data);…

Conflict between jquery and bootstrap

I have a code where I am including jquery files and bootstrap files in the header.php. I am running into issues where if I include the jquery file before bootstrap.js file, it messes up the other tabs …