Skip to content

Instantly share code, notes, and snippets.

@rmehner
Created May 1, 2013 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmehner/5496520 to your computer and use it in GitHub Desktop.
Save rmehner/5496520 to your computer and use it in GitHub Desktop.
Just a tiny helper function to get the query string out of an object (one level deep)
// note: this does not work for nested objects
var toQueryString = function(obj) {
return Object.keys(obj).map(function(key) {
return key + '=' + obj[key];
}).join('&');
};
toQueryString({}); // returns ''
toQueryString({foo: 'bar'}); // returns 'foo=bar'
toQueryString({billable: true, locked: false}); // returns 'billable=true&locked=false'
@sindresorhus
Copy link

This doesn't handle when a query string doesn't have a value, it should then return null.

See: https://github.com/sindresorhus/query-string

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