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

148
Visualizações
How can I add for each grouped object new key value in javascript?

Hi all I have following array


    const arr = [
      {
       ImageFileSet: [{ id: 1 }],
       LineItemFile: "new name",
       LineItemRef: "PDF",
       id: 44  
      },

       {
       ImageFileSet: [{ id: 7 }, { id: 8 }, { id: 9 }],
       LineItemFile: null,
       LineItemRef: "Image"
       id: 124
      },

    ];

I am trying to group array of objects in new array with following code

    const allFiles = arr .flatMap(({ ImageFileSet }) => [ImageFileSet]);

Output is

    [ { id: 1 }, { id: 7 }, { id: 8 }, { id: 9 } ]

Now how can I also add LineItemFile for each object ?

I want final result something like

    [ { id: 1,  LineItemFile: "new name", }, { id: 7,LineItemFile: null, }, { id: 8 , 
       LineItemFile: null,}, { id: 9 , LineItemFile: null,} ]

Please help me resolve this.

I looked into this article but it not helped.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you can do something like this

const arr = [{
    ImageFileSet: [{
      id: 1
    }],
    LineItemFile: "new name",
    LineItemRef: "PDF",
    id: 44
  },
  {
    ImageFileSet: [{
      id: 7
    }, {
      id: 8
    }, {
      id: 9
    }],
    LineItemFile: null,
    LineItemRef: "Image",
    id: 124
  },
];

const result = arr.flatMap(({ImageFileSet,LineItemFile}) =>
  ImageFileSet.map(d => ({ ...d, LineItemFile}))
)
console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

Something like:


    const allFiles = arr.flatMap((lineItem) => {
      return lineItem.ImageFileSet.map((imageFileSet) => (
          {...imageFileSet, LineItemFile: lineItem.LineItemFile}
        }))
   });


about 4 years ago · Juan Pablo Isaza Relatório

0

Try this :

const allFiles = arr.flatMap((obj) =>
    obj.ImageFileSet.flat().map((id) => ({
    ...id,
    LineItemFile: obj.LineItemFile
    }))
);

Output :

[
    {
        "id": 1,
        "LineItemFile": "new name"
    },
    {
        "id": 7,
        "LineItemFile": null
    },
    {
        "id": 8,
        "LineItemFile": null
    },
    {
        "id": 9,
        "LineItemFile": null
    }
]
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