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 stored 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.
2