Created
June 18, 2020 14:46
-
-
Save ibnumalik/20fb54ba397dcc96a9266a4c5592aa89 to your computer and use it in GitHub Desktop.
Convert object to url params
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
/** | |
* Convert object to url params. | |
* | |
* @param obj Object | |
*/ | |
function objToUrlParams(obj) { | |
return Object.entries(obj) | |
.map(([key, val]) => (!val ? null : `${key}=${val}`)) | |
.filter(Boolean) | |
.join('&'); | |
} | |
/* | |
{ | |
a: 'foo', | |
b: 'bar', | |
c: null, | |
d: false, | |
e: '' | |
} | |
will convert to | |
a=foo&b=bar | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment