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

180
Views
Using Array.map in nested object structure in Java Script

I have below object structure in array

[
  {
   "modules":set of modules here 
   "details": [
     {
       "name":"abc"
       "version":"1.2.3"
     },
     {
       "name":"def"
       "version":"2.3.4"
     },
     {
       "name":"ghi"
       "version":"4.5.6"
     }
   ]
  },
  {
   "modules":set of modules here 
   "details": [
     {
       "name":"jkl"
       "version":"7.8.9"
     },
     {
       "name":"mno"
       "version":"10.11.12"
     },
     {
       "name":"pqr"
       "version":"13.14.15"
     }
   ]  
  }
]

What I want to do is : get details array transformed into below format for each root object in master array as

"details": [
     {
       module:"jkl:7.8.9"
     },
     {
       module:"mno:10.11.12"
     },
     {
       module:"pqr:13.14.15"
     }
   ]  

So final array would be :

[
  {
   "modules":set of modules here 
   "details": [
     {
       "module":"abc:1.2.3"
     },
     {
       "module":"def:2.3.4"
     },
     {
       "module":"ghi:4.5.6"
     }
   ]
  },
  {
   "modules":set of modules here 
   "details": [
     {
       "module":"jkl:7.8.9"
     },
     {
       "module":"mno:10.11.12"
     },
     {
       "module":"pqr:13.14.15"
     }
   ]  
  }
]

What I have tried and is working is :

rootArray.forEach((entry)=> {
    let line = entry.details.map((detailEntry)=>{
    //merge detailEntry into single line 
    })
    entry.details = line 
});

My question is : is this a right approach or is there any better solution available ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const data = [
  {
   "modules": "set of modules here",
   "details": [
     {
       "name":"abc",
       "version":"1.2.3"
     },
     {
       "name":"def",
       "version":"2.3.4"
     },
     {
       "name":"ghi",
       "version":"4.5.6"
     }
   ]
  },
  {
   "modules": "set of modules here",
   "details": [
     {
       "name":"jkl",
       "version":"7.8.9"
     },
     {
       "name":"mno",
       "version":"10.11.12"
     },
     {
       "name":"pqr",
       "version":"13.14.15"
     }
   ]  
  }
]

const result = data.map( item => ({modules: item.modules, details: item.details.map(i => {
  return { module: `${i["name"]}:${i["version"]}` }
})}))

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

your approach seems correct.

there is another way to achieve the expected structure, making the copy of the initial array however instead of modifying the existing.

const result = rootArray.map(({ details, ...rest }) => ({
    details: details.map(/*transform*/),
    ...rest
));
about 4 years ago · Juan Pablo Isaza Report

0

Two maps is probably the right approach. It will return a new array rather than mutating the original array.

map over the main data array, and then assign the result of mapping over details to the details property of the object that you're returning on each iteration.

const data=[{modules:"set of modules here",details:[{name:"abc",version:"1.2.3"},{name:"def",version:"2.3.4"},{name:"ghi",version:"4.5.6"}]},{modules:"set of modules here",details:[{name:"jkl",version:"7.8.9"},{name:"mno",version:"10.11.12"},{name:"pqr",version:"13.14.15"}]}];

const out = data.map(obj => {
  return {
    modules: obj.modules,
    details: obj.details.map(detail => {
      const { name, version } = detail;
      return { module: `${name}:${version}` };
    })
  };
});

console.log(out);

Additional documentation

  • Destructuring assignment

  • map

  • Template/string literals

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!