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

199
Vistas
javascript - find nested property from javascript object

I might have two type of payloads.

const payload = {
   "firstName":"Steven",
   "lastName":"Smith"
}

or

const payload = {
   "personalDetails": {
       "firstName":"Steven",
       "lastName":"Smith"
    }
}

How can I retrieve firstName from the payload during a REST API call.

const { firstName } = payload;  

The above code will work for the first payload but it won't for the second payload since firstName is nested inside personalDetails. Is there a clean of retrieving it in ES6?

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

0

const { personalDetails: { firstName } } = payload;  

More about destructuring nested objects.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You cound take personalDetails property if not undefined or null or just the variable directly.

const { firstName } = payload.personalDetails ?? payload;
about 4 years ago · Juan Pablo Isaza Denunciar

0

Strange scenario you have here but this might solve the issue:

const { firstName } = payload.personalDetails ? payload.personalDetails : payload;

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