FuruholmAnton
11/15/2017 - 3:51 PM

deepSerialize.js

/**
 * 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;
  }
}