Skip to content

Instantly share code, notes, and snippets.

@ibnumalik
Created June 18, 2020 14:46
Show Gist options
  • Save ibnumalik/20fb54ba397dcc96a9266a4c5592aa89 to your computer and use it in GitHub Desktop.
Save ibnumalik/20fb54ba397dcc96a9266a4c5592aa89 to your computer and use it in GitHub Desktop.
Convert object to url params
/**
* 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