【问题标题】:need to transform array of objects from form to another in angular需要以角度将对象数组从形式转换为另一个
【发布时间】:2021-10-02 21:24:24
【问题描述】:

输入数组:

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

我需要将输入数组对象转换为输出下面在 Angular 打字稿中给出的数组对象。 我尝试使用纯 JavaScript,但为此我必须编写大量代码,所以我想简化代码来实现这一点。

为了实现这一点,我必须编写多个 for 循环和 if 条件,但我想避免它,并且解决方案应该更通用,以便我可以通过更改密钥在多个地方使用相同的解决方案。如果解决方案使用 loadsh 来满足此要求并且不需要纯 JavaScript 方法会更好。

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    你可以尝试使用reduce函数来避免使用if和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)

    【讨论】:

    • 谢谢,这行得通!但我还在数组中包含 time 属性,如下所示 [ { batchScheduleId: 1, Time:[12:00, 12.30] dayId: [1,2] }, { batchScheduleId: 2, Time:[13:00,13:30 ], dayId: [1,2] } ]
    • 您可以查看修改后的解决方案
    • 谢谢。我需要对输入数组输出数组输出数组再进行一次转换:[ { day: 1, batchtime: [ { time: 12:00, 12.30 batch: 1 } ] } ]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2015-05-11
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多