Created
April 10, 2019 12:42
-
-
Save RickJP/99155be0a3a196dbf24668437b565034 to your computer and use it in GitHub Desktop.
JS - Reverse lookup object with array
This file contains hidden or 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
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