Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
need to transform array of objects from form to another in angular

inputs array:

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

i need transform input array objects to output array objects given below in angular typescript. i tried using plain JavaScript but for that i have to write lots of code so i want simplify code to achieve this.

to acheive this i have to write multiple for loops and if condition but i want to avoid it and the solution should more generic so that i can use the same solution in multiple places just by changing the key. it would be better if the solution uses loadsh for this requirement and plain JavaScript approach is not required.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try to use reduce function to avoid using the if and the 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda