Skip to content

Instantly share code, notes, and snippets.

@DIY0R
Last active July 29, 2024 10:56
Show Gist options
  • Save DIY0R/7fdbf323b29b23ae70a9c8135000314c to your computer and use it in GitHub Desktop.
Save DIY0R/7fdbf323b29b23ae70a9c8135000314c to your computer and use it in GitHub Desktop.
JavaScript
function convertQueryToMap(query) {
const result = {};
if (!query) return result;
query.split('&').forEach((pair) => {
const [pathway, value] = pair.split('=');
const pathwaySplit = pathway.split('.');
let obj = result;
pathwaySplit.forEach((prop, index) => {
if (index === pathwaySplit.length - 1) {
obj[prop] = decodeURIComponent(value);
} else {
if (!obj[prop]) obj[prop] = {};
obj = obj[prop];
}
});
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment