Skip to content

Instantly share code, notes, and snippets.

@RickJP
Created April 10, 2019 12:42
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 RickJP/99155be0a3a196dbf24668437b565034 to your computer and use it in GitHub Desktop.
Save RickJP/99155be0a3a196dbf24668437b565034 to your computer and use it in GitHub Desktop.
JS - Reverse lookup object with array
resourceMap = {
"a": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"b": [11, 12],
"c": [21, 23],
"d": [54, 55, 56, 57, 510]
};
var reverseMap = {};
for(var propName in resourceMap)
{
var numsArr = resourceMap[propName];
numsArr.forEach(function(num){
reverseMap[num]=propName;
});
}
console.log(reverseMap[54]); //'d'
=======================================================================================================================================
var reverseMap2 = Object.keys(resourceMap).reduce((acc, propName) =>
resourceMap[propName].reduce((a, num) => {
a[num] = propName;
return a;
}, acc), {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment