Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
   "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 ?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading