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

184
Views
¿Cómo recalcular una matriz de objetos a partir de objetos anidados?

Tengo una matriz de objetos donde Citems es una matriz de objetos. Cada objeto tiene estado activado o desactivado y hora.

 [ { Chapter: [ { Cname: 'chapter 1', Citems: [{status: 'on', time: 30},{status: 'on', time: 60}], }, { Cname: 'chapter 2', Citems: [{status: 'on', time: 30},{status: 'off', time: 60}] } ], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' },{ Chapter: [ { Cname: 'chapter 1', Citems: [{status: 'on', time: 30},{status: 'on', time: 60}], }, { Cname: 'chapter 2', Citems: [{status: 'on', time: 30},{status: 'off', time: 60}] } ], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' },{ Chapter: [ { Cname: 'chapter 1', Citems: [{status: 'on', time: 30},{status: 'on', time: 60}], }, { Cname: 'chapter 2', Citems: [{status: 'on', time: 30},{status: 'off', time: 60}] } ], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' } ]

Quiero generar una matriz u objeto a partir de él que muestre el tiempo total para cada estado como se muestra a continuación

 [{ on: 120, off: 60 }, { on: 120, off: 60 }, { on: 120, off: 60 }]

Intenté usar el mapa y reducirlo al no obtener el resultado deseado.

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

0

Estabas en el camino correcto usando map() y reduce() . Una llamada adicional a flatMap() y algo de desestructuración lo reúne todo:

 const result = data.map(({ Chapter }) => Chapter .flatMap(({ Citems }) => Citems) .reduce((a, { status, time }) => ({ ...a, [status]: (a[status] || 0) + time }), {}) );

Fragmento completo:

 const data = [{ Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'on', time: 60 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] } ], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' }, { Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'on', time: 60 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] } ], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' }, { Chapter: [{ Cname: 'chapter 1', Citems: [{ status: 'on', time: 30 }, { status: 'on', time: 60 }], }, { Cname: 'chapter 2', Citems: [{ status: 'on', time: 30 }, { status: 'off', time: 60 }] } ], name: 'Something', description: 'jfdgljfgdfjgldfkjglfd' }]; const result = data.map(({ Chapter }) => Chapter .flatMap(({ Citems }) => Citems) .reduce((a, { status, time }) => ({ ...a, [status]: (a[status] || 0) + time }), {}) ); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Lo hice usando map y flatMap .

 data.map(({ Chapter }) => { let on = 0; let off = 0; Chapter.flatMap(({ Citems }) => Citems).map((item) => { if (item.status === "on") { on += item.time; } else { off += item.time; } }); return { on, off }; }); 

 const data = [ { Chapter: [ { Cname: "chapter 1", Citems: [ { status: "on", time: 30 }, { status: "on", time: 60 } ] }, { Cname: "chapter 2", Citems: [ { status: "on", time: 30 }, { status: "off", time: 60 } ] } ], name: "Something", description: "jfdgljfgdfjgldfkjglfd" }, { Chapter: [ { Cname: "chapter 1", Citems: [ { status: "on", time: 30 }, { status: "on", time: 60 } ] }, { Cname: "chapter 2", Citems: [ { status: "on", time: 30 }, { status: "off", time: 60 } ] } ], name: "Something", description: "jfdgljfgdfjgldfkjglfd" }, { Chapter: [ { Cname: "chapter 1", Citems: [ { status: "on", time: 30 }, { status: "on", time: 60 } ] }, { Cname: "chapter 2", Citems: [ { status: "on", time: 30 }, { status: "off", time: 60 } ] } ], name: "Something", description: "jfdgljfgdfjgldfkjglfd" } ]; const result = data.map(({ Chapter }) => { let on = 0; let off = 0; Chapter.flatMap(({ Citems }) => Citems).map((item) => { if (item.status === "on") { on += item.time; } else { off += item.time; } }); return { on, off }; }); console.log(result);

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!