I have a JS Object Literal (logGroupMap) containing alarm names as keys, and their log groups as values. Alarm names are passed in with consistent names of varying length but with generated characters at the end each time, so I'm trying to see if the string includes a key from the logGroupMap object literal, and if so I want to get the value associated with that key.
let logGroup = (alarmName) => {
Object.keys(logGroupMap).forEach((key) => {
if (alarmName.includes(key)) { logGroup = logGroupMap.key; }
});
};
console.log(logGroup(messageDetails.AlarmName));
Currently this is returning undefined in all cases.
Use the Array.find()
method to search for a matching key.
To access a property dynamically, use logGroupMap[key]
. logGroupMap.key
returns the value of the property named key
, it doesn't use key
as a variable.
function logGroup(alarmName) {
let found = Object.keys(logGroupMap).find((key, value) => alarmName.includes(key));
if (found) {
return logGroupMap[key];
}
}