Skip to content

Instantly share code, notes, and snippets.

@anton-karlovskiy
Last active July 12, 2020 20:50
Show Gist options
  • Save anton-karlovskiy/3c75950643a7d03b4e7170cc870e2154 to your computer and use it in GitHub Desktop.
Save anton-karlovskiy/3c75950643a7d03b4e7170cc870e2154 to your computer and use it in GitHub Desktop.
How to create the query parameter string by combining a JS object
// RE: https://github.com/sindresorhus/query-string
// RE: https://bundlephobia.com/result?p=query-string@6.13.1
// RE: https://github.com/unshiftio/querystringify
// RE: https://github.com/Gozala/querystring
const serializeToQueryParams = (queryObject, prefix = '') => {
const queryString = [];
for (const key in queryObject)
if (queryObject.hasOwnProperty(key)) {
queryString.push(encodeURIComponent(key) + '=' + encodeURIComponent(queryObject[key]));
}
return `${prefix}?${queryString.join('&')}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment