Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda