Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

43
Vistas
returning a value if a String includes the key

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.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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];
  }
}

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.