Last active
August 29, 2015 14:24
-
-
Save cdmckay/cf4bb867ebeb630c027c to your computer and use it in GitHub Desktop.
A snippet to serialize an object in to query parameters
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
function serialize(object) { | |
return Object.keys(object).map(function (key) { | |
return encodeURIComponent(key) + '=' + encodeURIComponent(object[key]); | |
}).join('&'); | |
} | |
// Example | |
var params = { | |
username: 'cdmckay', | |
password: 'hunter2' | |
}; | |
var serialized = serialize(params); | |
// serialized = "username=cdmckay&password=hunter2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment