Created
April 7, 2018 06:33
-
-
Save BobRupholdt/f1c426ae94959eca1d670bda4e3f0a3b to your computer and use it in GitHub Desktop.
Break large JSON into an array of smaller objects. I made this for Google Apps Script to break large API results into smaller chunks for moving into Firebase. This assumes that each key in the object is a record.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function splitObject_TEST(){Logger.log(splitObject({1:11,2:22,3:33,4:44,5:55,6:66,7:77}, 3))} | |
function splitObject(data, size){ | |
if(Object.keys(data).length <= size) | |
return [data]; | |
var count = 0; | |
var out = []; | |
for (var key in data) { | |
var bowl = Math.floor((count++)/size); | |
if( ! out[bowl] ) out[bowl] = {}; | |
out[bowl][key] = data[key]; | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment