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

216
Views
Parsing dynamic JSON structure in JavaScript using .map to create specific arrays?

I am looking to create a function that will iterate through JSON and fetch an ID. If there are details within the object containing the ID, attach them to that array as well.

Something like json?.cars?.map(m => 'Manufacturer.' + m?.manufacturer) retrieves the manufacturers into separate arrays, but I also wanted to retrieve the 'Model' if available and put it into the Manufacturer array so it might look like this.

[[ Manufacturer.Toyota ], Model.Supra, Model.Rav4 ]] [ Manufacturer.BMW ] [ Manufacturer.Mercedes ]

{
  'Cars': [
    {
      'Manufacturer': 'Toyota',
      'Details': [
        {
          'Model': 'Supra',
          'Transmission': 'Manual',
          'Colour': 'Red'
        },
        {
          'Model': 'Rav4',
          'Transmission': 'Automatic',
          'Colour': 'White'
        }
      ]
    },
    {
      'Manufacturer': 'BMW'
    },
    {
      'Manufacturer': 'Mercedes'
    }
  ]
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Couldn't think of a easier way, but try this:

const array = Cars.map(obj => {
  return [[obj.Manufacturer], obj?.Details !== undefined ? obj.Details.map(details => details.Model) : null].filter(obj => obj !== null)
})
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!