Skip to content

Instantly share code, notes, and snippets.

@FuruholmAnton
Last active September 18, 2018 16:09
Show Gist options
  • Save FuruholmAnton/3b146a2baf50ee825300930aaba2447c to your computer and use it in GitHub Desktop.
Save FuruholmAnton/3b146a2baf50ee825300930aaba2447c to your computer and use it in GitHub Desktop.
/**
* Serializes the object and arrays and objects of the main object.
*
* @param {Object} data - the object to serialize
* @param {String} prefix - used internally to keep track of the nesting
*/
function deepSerialize(data, prefix = '') {
if (typeof data == 'function') return prefix != '' ? prefix + '=null' : '';
const array = [];
if (data instanceof Object) {
for (let key of Object.keys(data)) {
let value = data[key];
if (typeof value != 'function') {
array.push(deepSerialize(value, prefix !== '' ? prefix + `[${key}]` : key));
}
}
return array.join('&');
} else {
return prefix + '=' + data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment