Last active
December 4, 2021 08:29
-
-
Save tobiasroeder/c73eee41d8af8d366d2973dadfc42fb4 to your computer and use it in GitHub Desktop.
Convert a string (location.hash) into an object.
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
/** | |
* convert string (hash) to object | |
* @param {string} hash | |
* @returns {object} | |
*/ | |
function hashToObject(hash) { | |
let obj = {}; | |
hash = hash.slice(1); | |
let params = hash.split(','); | |
params.forEach(param => { | |
if (!param) return; | |
param = param.split('='); | |
obj[param[0]] = param[1]; | |
}); | |
return obj; | |
} | |
// demo | |
let hash = '#id=123,lang=de,,user=test'; | |
console.log(hashToObject(hash)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment