Skip to content

Instantly share code, notes, and snippets.

@tjmehta
Last active October 31, 2024 23:25
Show Gist options
  • Save tjmehta/9204891 to your computer and use it in GitHub Desktop.
Save tjmehta/9204891 to your computer and use it in GitHub Desktop.
Object to Querystring - JavaScript, JS, JSON
function objectToQuerystring (obj) {
return Object.keys.reduce(function (str, key, i) {
var delimiter, val;
delimiter = (i === 0) ? '?' : '&';
key = encodeURIComponent(key);
val = encodeURIComponent(obj[key]);
return [str, delimiter, key, '=', val].join('');
}, '');
}
@aslan0402
Copy link

const objectToQuerystring = obj => new URLSearchParams(obj).toString();

lol

@zm-rylee
Copy link

const objectToQuerystring = obj => new URLSearchParams(obj).toString();

lol

That doesn't work with objects that contain arrays or other objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment