• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

84
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.

about 3 years 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];
  }
}

about 3 years 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 vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda