Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda