Created
May 1, 2013 16:50
-
-
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)
This file contains 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
// 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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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