Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created July 5, 2023 18:13
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 cferdinandi/d12247d967722e110c534160a2019cd1 to your computer and use it in GitHub Desktop.
Save cferdinandi/d12247d967722e110c534160a2019cd1 to your computer and use it in GitHub Desktop.
<!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