Created
April 28, 2020 09:58
-
-
Save claudiohilario/0d35eeadc77e69c13db4a696bda9285f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function buildQueryParams(params = {}) { | |
const arrParams = Object.keys(params); | |
if(!arrParams.length) { | |
return ''; | |
} | |
return `?${arrParams.map(param => `${param}=${params[param]}`).join('&')}`; | |
} | |
console.log(buildQueryParams()); // '' | |
console.log(buildQueryParams({})); // '' | |
console.log(buildQueryParams({a: 'test'})); // '?a=test' | |
console.log(buildQueryParams({a: 'test', bb: 'testb'})); // ?a=test&bb=testb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment