Last active
September 18, 2018 16:09
-
-
Save FuruholmAnton/3b146a2baf50ee825300930aaba2447c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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