Skip to content

Instantly share code, notes, and snippets.

@tobiasroeder
Last active December 4, 2021 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasroeder/c73eee41d8af8d366d2973dadfc42fb4 to your computer and use it in GitHub Desktop.
Save tobiasroeder/c73eee41d8af8d366d2973dadfc42fb4 to your computer and use it in GitHub Desktop.
Convert a string (location.hash) into an object.
/**
* 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