Last active
July 29, 2024 10:56
-
-
Save DIY0R/7fdbf323b29b23ae70a9c8135000314c to your computer and use it in GitHub Desktop.
JavaScript
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
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