Skip to content

Instantly share code, notes, and snippets.

@OlegKorn
Last active October 7, 2022 04:37
Show Gist options
  • Save OlegKorn/d151f51f2c51ed832bd713f989aafec6 to your computer and use it in GitHub Desktop.
Save OlegKorn/d151f51f2c51ed832bd713f989aafec6 to your computer and use it in GitHub Desktop.
JS: finding a value by a key in dictionary
function isThereEquivalent(equivalentsArr, value) {
try {
var equivalent = Object.entries(equivalentsArr).find(([key]) => key === value.toString())[1]
return equivalent
}
catch (e) {
if (e instanceof TypeError) {
return false
}
}
}
/*
EXAMPLE
var equivalents = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M"
}
isThereEquivalent(equivalents, 90) returns XC
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment