Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

0

const { personalDetails: { firstName } } = payload;  

More about destructuring nested objects.

about 4 years ago · Juan Pablo Isaza Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!