stringfyQueryString.js 625 B

123456789101112131415161718192021222324
  1. /**
  2. *
  3. * @desc 对象序列化
  4. * @param {Object} obj
  5. * @return {String}
  6. */
  7. function stringfyQueryString(obj) {
  8. if (!obj) return '';
  9. var pairs = [];
  10. for (var key in obj) {
  11. var value = obj[key];
  12. if (value instanceof Array) {
  13. for (var i = 0; i < value.length; ++i) {
  14. pairs.push(encodeURIComponent(key + '[' + i + ']') + '=' + encodeURIComponent(value[i]));
  15. };
  16. continue;
  17. }
  18. pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
  19. };
  20. return pairs.join('&');
  21. };
  22. module.exports = stringfyQueryString;