Home > database >  Using Array.map in nested object structure in Java Script
Using Array.map in nested object structure in Java Script

Time:07-11

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 ?

CodePudding user response:

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)

CodePudding user response:

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
));

CodePudding user response:

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

  • Related