I have a map of arrays of numbers in JavaScript. My goal is to get the key of the value that contains a certain number. I'm also open to a different data structure that might be more efficient.
let bookCategory = {
"fantasy": [10064, 10066, 10071],
"scifi": [10060, 10037, 10061],
"history": [10001, 10003, 10004, 10005],
"biography": [10032, 10006, 10002, 10028, 10009, 10030, 100031],
"educational": [10025]
};
Each number will only ever appear once, but each array can contain close to a hundred numbers and it may grow substantially from there. The arrays could be immutable as my data is static.
Right now I have this, but it doesn't seem terribly efficient.
let category;
let keys = _.keys(categories);
let theNumber = 10032;
for(let j = 0; j < keys.length; j++) {
if(_.includes(categories[keys[j]], theNumber)) {
category = keys[j];
break;
}
}
10001and300be?undefined.