I have a string and I have a list of dictionaries in Java/Typescript and I am trying to find that string in whichever dictionary it is in (it can only be in one entry) and return a value stored in the number key in the dictionary. I am doing that by iterating through my list and comparing the value storred to my input string. All dictionary label and number entries are unique fwiw.
var myString = "ABC"
var output = 0
const listOfDicts = [
{label: "ABC", number: 10}
{label: "DEF", number: 20}
{label: "GHI, number: 30}
]
listOfDicts.forEach((e) => {
if (e.label === myString ){
output = e.number
}}
Is this the most efficient way of assigning 10 to my output or is there a clever 1 line way of doing this that I am not aware of? Note if my structure was a Map I could just use the .get(myString) method to return the corresponding value, but I do not have a map so here we are. For bonus points do you know why
myString = "def"
listOfDicts.forEach((e) => {
if (e.label.toLowerCase === myString.toLowerCase ){
output = e.number
}}
might return 30 under some circumstances when myString is NOT “GHI”?