-
-
Save cferdinandi/d12247d967722e110c534160a2019cd1 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>buildQuery()</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
</head> | |
<body> | |
<script> | |
/** | |
* Build a query string from an object of data | |
* (c) Chris Ferdinandi, MIT License, https://gomakethings.com | |
* @param {Object} data The data to turn into a query string | |
* @return {String} The query string | |
*/ | |
function buildQuery (data) { | |
return new URLSearchParams(data).toString(); | |
} | |
let petfinderData = { | |
key: '12345', | |
shelterID: 'abc00', | |
count: 20, | |
animals: ['dogs', 'cats'] | |
}; | |
// returns "key=12345&shelterID=abc00&count=20&animals=dogs%2Ccats" | |
let query = buildQuery(petfinderData); | |
console.log(query); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment