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

284
Visualizações
¿Cómo extraigo una matriz en una matriz para obtener otra matriz?

¿Cómo extraigo la matriz en una matriz y la estructuro para que sea una matriz completa?

Aporte:

 const input = [{ categoryName: "Chinese food", tabs: [{ header: "Chicken Rice", content: "Hainanese chicken rice is a dish of poached chicken and seasoned rice" }, { header: "Dim sum", content: "large range of small Chinese dishes that are traditionally enjoyed in restaurants" } ] }, { categoryName: "Italian food", tabs: [{ header: "Pizza", content: "Dish of Italian origin consisting of a usually round" }] }, ]

Salida (necesita extraer todos los encabezados para convertirse en esta matriz)

 const output = [ { "categoryName": "Chinese food", "header": "Chicken Rice", }, { "categoryName": "Chinese food", "header": "Dim sum", }, { "categoryName": "Italian food", "header": "Pizza" }, ]

intenté esto pero parece que no puedo agregar el nombre de categoría dentro

 const getHeadersWithId = arr => ( arr.flatMap( // iterate over 'input' array and 'flat'-ten the result ({tabs}) => (tabs.map( // de-structure 'tabs' & iterate over it ({header}) => ({header}) // de-structure 'header' & retain it to intermediate-result array )) ).map( ({header}, idx) => ({ // iterate with index 'idx' id: idx+1, // generate the 'id' and store header // header from intermediate-result array element }) ) );

Esto es lo que obtuve cuando ejecuté este trozo de código

 [ { "id": 1, "header": "Chicken Rice" }, { "id": 2, "header": "Dim sum" }, { "id": 3, "header": "Pizza" } ]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

En mi opinión, no necesita el segundo map() . Simplemente use el nombre de categoryName y páselo a su map() .

Desarrollando tu código:

 const input = [{ categoryName: "Chinese food", tabs: [{ header: "Chicken Rice", content: "Hainanese chicken rice is a dish of poached chicken and seasoned rice" }, { header: "Dim sum", content: "large range of small Chinese dishes that are traditionally enjoyed in restaurants" } ] }, { categoryName: "Italian food", tabs: [{ header: "Pizza", content: "Dish of Italian origin consisting of a usually round" }] }, ] const getHeadersWithId = arr => ( arr.flatMap( // iterate over 'input' array and 'flat'-ten the result ({tabs,categoryName}) => tabs.map( // de-structure 'tabs' & iterate over it ({header}) => ({header,categoryName}) // de-structure 'header' & retain it to intermediate-result array ) ) ); console.log(getHeadersWithId(input));

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