Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
¿Cómo puedo agregar para cada objeto agrupado un nuevo valor clave en javascript?

Hola a todos, tengo la siguiente matriz

 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 }, ];

Estoy tratando de agrupar una matriz de objetos en una nueva matriz con el siguiente código

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

La salida es

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

Ahora, ¿cómo puedo agregar LineItemFile para cada objeto?

Quiero un resultado final algo como

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

Por favor ayúdame a resolver esto.

Investigué este artículo pero no ayudó.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

puedes hacer algo como esto

 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 Report

0

Algo como:

 const allFiles = arr.flatMap((lineItem) => { return lineItem.ImageFileSet.map((imageFileSet) => ( {...imageFileSet, LineItemFile: lineItem.LineItemFile} })) });
about 4 years ago · Juan Pablo Isaza Report

0

Prueba esto :

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

Producción :

 [ { "id": 1, "LineItemFile": "new name" }, { "id": 7, "LineItemFile": null }, { "id": 8, "LineItemFile": null }, { "id": 9, "LineItemFile": null } ]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!