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

148
Views
necesita transformar una matriz de objetos de forma a otra en angular

matriz de entradas:

 weekSchedules: [ { dayId: 1, daySchedules: [ { batchScheduleId: 1, Time: 12:00 }, { batchScheduleId: 1, ime: 12:30 }, { batchScheduleId: 2, Time: 13:00 }, { batchScheduleId: 2, Time: 13:30 } ] }, { dayId: 2, daySchedules: [ { batchScheduleId: 1, Time: 12:00 }, { batchScheduleId: 1, Time: 12:30 }, { batchScheduleId: 2, Time: 13:00 }, { batchScheduleId: 2, Time: 13:30 } ] } } ] output array: [ { batchScheduleId: 1, Time:[12:00,12.30], dayId: [1,2] }, { batchScheduleId: 2, Time:[12:00,12.30], dayId: [1,2] } ]

Necesito transformar objetos de matriz de entrada para generar objetos de matriz que se indican a continuación en mecanografiado angular. Intenté usar JavaScript simple, pero para eso tengo que escribir mucho código, así que quiero simplificar el código para lograrlo.

para lograr esto, tengo que escribir múltiples bucles for y si la condición, pero quiero evitarla, y la solución debería ser más genérica para poder usar la misma solución en varios lugares simplemente cambiando la clave. sería mejor si la solución usa loadsh para este requisito y no se requiere un enfoque simple de JavaScript.

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

0

Puede intentar usar la función de reducción para evitar el uso de if y for

 const inputObject = {weekSchedules:[{dayId:1,daySchedules:[{batchScheduleId:1,Time:"12: 00"},{batchScheduleId:1,Time:"12: 30"},{batchScheduleId:2,Time:"13: 00"},{batchScheduleId:2,Time:"13: 30"}]},{dayId:2,daySchedules:[{batchScheduleId:1,Time:"12: 00"},{batchScheduleId:1,Time:"12: 30"},{batchScheduleId:2,Time:"13: 00"},{batchScheduleId:2,Time:"13: 30"}]}]} const flattenedBatch = inputObject.weekSchedules .reduce((prev, next) => [ ...prev, ...next.daySchedules.map(({batchScheduleId, Time}) => ({batchScheduleId, dayId: next.dayId, Time})) ], []) const groupedData = flattenedBatch.reduce((prev, next) => { const prevValue = prev?.[next.batchScheduleId] ? prev[next.batchScheduleId].dayId : []; prevValue.push(next.dayId) const prevTime = prev?.[next.batchScheduleId] ? prev[next.batchScheduleId].Time : []; prevTime.push(next.Time) return {...prev, [next.batchScheduleId]: { batchScheduleId: next.batchScheduleId, dayId: [...new Set(prevValue)], Time: [...new Set(prevTime)] }} }, {}); const outputData = Object.values(groupedData) console.log(outputData)

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!