Skip to content

Instantly share code, notes, and snippets.

@oscarmarina
Created April 9, 2024 09:03
Show Gist options
  • Save oscarmarina/0eb66cd59b41077f0d8c74517be6740c to your computer and use it in GitHub Desktop.
Save oscarmarina/0eb66cd59b41077f0d8c74517be6740c to your computer and use it in GitHub Desktop.
converting-a-url-object-to-a-plain-object-in-java-scrip
// https://www.abeautifulsite.net/posts/converting-a-url-object-to-a-plain-object-in-java-script/
const url = new URL('https://api.chucknorris.io/jokes/random');
function urlToPlainObject(url) {
const plainObject = {};
for (const key in url) {
if (typeof url[key] === 'string') {
plainObject[key] = url[key];
}
}
return plainObject;
}
console.log(urlToPlainObject(url));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment