Last active
March 25, 2017 16:38
-
-
Save bj7/ce63c0a7f2ebc8d29f43db00eccca369 to your computer and use it in GitHub Desktop.
Small recursive routine to deeply parse any object and convert all arrays into string lists. Helps when dealing with servers that limit http data parameters.
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
/** | |
* Recurse object and stringify any arrays. This helps when passing paremeters | |
* to a server since some servers may limit the number of distinct items in http requests. | |
* @param {Object} obj Object to parse | |
* @return {Object} Return the parsed object | |
*/ | |
function jsonDeepStringify(obj) { | |
if (Object.prototype.toString.call(obj) == '[object Array]') { | |
obj = obj.join(','); | |
} else if (typeof (obj) == 'object') { | |
for (let i in obj) { | |
if (typeof (obj[i]) == 'object') { | |
obj[i] = jsonDeepStringify(obj[i]); | |
} | |
} | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edit to better handle cases when the incoming parameter is already an object.