Last active
September 7, 2020 09:42
-
-
Save lgk-bsw/6ad7056dd1ec33a840852b17d2bced67 to your computer and use it in GitHub Desktop.
A handy way to convert a JS object to a query string you can pass to an API
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
const params = {type: "G6.USID", key: "AllgFilter", listPkey: "~ 000G6.USID"}; | |
const query = Object.keys(params) | |
.map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k])) | |
.join("&") | |
// Will become: type=G6.USID&key=AllgFilter&listPkey=~%20%20%20000G6.USID | |
// Usage | |
fetch(`https://my-api.com/?${query}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment