Created
April 11, 2024 13:53
-
-
Save maiconrs95/cd8db20046df03ebe4f42eafeb019274 to your computer and use it in GitHub Desktop.
Convert object to search 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
/** | |
* Converts an object into formatted URL query parameters. | |
* @param {Object} obj - The object to be converted into query parameters. | |
* @returns {string} A string containing the formatted query parameters. | |
*/ | |
export function objectToQueryParams(obj: { [key: string]: string | number | boolean }): string { | |
const queryParams: string[] = []; | |
for (const key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
queryParams.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`); | |
} | |
} | |
return queryParams.join('&'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment