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

164
Views
Cómo extraer un objeto de arrayList y agregarlo a otro arrayList

Estoy tratando de convertir una lista de arreglos en otra modificando uno de los objetos dentro

 let array1 = [ { name: "test", address: "testaddress", state: { 2022: { January: { month_state: "pending" }, }, }, }, { name: "test2", address: "testaddress2", state: { 2022: { January: { month_state: "pending" }, }, }, }, ];

Y quiero convertirme en esto, ¿cuál es la forma correcta? Gracias.

 let array2 = [ { name: "test", address: "testaddress", 2022: { January: { month_state: "pending" }, }, }, { name: "test2", address: "testaddress2", 2022: { January: { month_state: "pending" }, }, }, ];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

let array2 = array1.map(({ state, ...rest }) => ({ ...rest, ...state }))

Este código servirá.

about 4 years ago · Juan Pablo Isaza Report

0

Hay varias formas de lograr lo que estás pidiendo.

Te mostraré usando el enfoque funcional con .map

 let array1 = [ { name: "test", address: "testaddress", state: { 2022: { January: { month_state: "pending" } } } }, { name: "test2", address: "testaddress2", state: { 2022: { January: { month_state: "pending" } } } } ]; let array2 = array1.map((el) => { const tmp = { name: el.name, adress: el.adress }; // Here we iterate el.state, so we can extract each year. /* We need to dynamically generate a property name (each year). So we use a feature called "computed property names (ES2015)" */ for (const prop in el.state) { tmp[prop] = el.state[prop]; } return tmp; }); console.log(array2);

También podría usar un bucle for, que también es un enfoque válido.

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!