Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
¿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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda