Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Javascript find and map values based on scheme throwing undefined

I'm working with Javascript to build a mapping function, that, given a scheme should find the mapper's object's value within an application variable.

My attempt at doing this results in an undefined error and I'm trying to figure out what I need to change to make it work correctly, I've put together a JS fiddle that can be found here as well.

const application = {
  state: {
    outgoings: {
      credit: 0,
      food: 0
    }
  }
}

const mapper = {
  "AppFoodSpend": "outgoings.food",
  "AppCreditSpend": "outgoings.credit"
}

for (const field in mapper) {
    console.log(application.state[mapper[field]])
    // expected results should be credit 0 and food 0
}

For further context, I have an object called application which contains some fields, they may not always be in the same order. My mapper object contains some key/value pairs, and it's the value that I'd like to try and find in application. for for instance, my console log should be retrieving something like:

  • application.state.outgoings.food
  • application.state.outgoings.credit

They may not always be in this order, so I need to (on each iteration of the mapper) find the key in my application variable.

What do I need to change here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Strings like 'outgoings.food' are not valid to navigate a data structure. You could use some "lenses" lib or write something like this...

const mapper = {
  "AppFoodSpend": ["outgoings", "food"],
  "AppCreditSpend": ["outgoings", "credit"]
}

for (const field in mapper) {
    console.log(
        mapper[field].reduce(
            (ctx, path) => ctx[path],
            application.state
        )
    )
}
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda