Skip to content

Instantly share code, notes, and snippets.

@maiconrs95
Created April 11, 2024 13:53
Show Gist options
  • Save maiconrs95/cd8db20046df03ebe4f42eafeb019274 to your computer and use it in GitHub Desktop.
Save maiconrs95/cd8db20046df03ebe4f42eafeb019274 to your computer and use it in GitHub Desktop.
Convert object to search params
/**
* 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