【问题标题】:How to extend/expand array of objects based on property如何根据属性扩展/扩展对象数组
【发布时间】:2020-11-11 22:52:51
【问题描述】:

我正在尝试转换

来源:

[{
  dayIds: [1],
  timeRanges: [{
  end: 1200,
  start: 800
}],
  timezoneType: [{
  timezone_type: "AM/PM"
}]
}, {
  dayIds: [1, 2, 3],
  timeRanges: [{
  end: 720,
  start: 360
}, {
  end: 230,
  start: 160
}],
  timezoneType: [{
  timezone_type: "AM/PM"
}, {
  timezone_type: "AM/PM"
}, {
  timezone_type: "AM/PM"
}]
}]

预期:

[{
  dayIds: [1],
  endTime: 1200,
  startTime: 800,
  timezone_type: "AM/PM"
}, {
  dayIds: [1, 2, 3],
  endTime: 720,
  startTime: 360
  timezone_type: "AM/PM"

}, {
  dayIds: [1, 2, 3],
  endTime: 230,
  startTime: 160,
  timezone_type: "AM/PM"
}]

我很难将上面的源数组转换为预期的平面数组,其中每个 timeRange 对象都必须构造新对象。

任何改进数组操作以及解决上述问题的建议。

这是我到目前为止所尝试的。

let array = [{
  dayIds: [1],
  timeRanges: [{
  end: 1200,
  start: 800
}],
  timezoneType: [{
  timezone_type: "AM/PM"
}]
}, {
  dayIds: [1, 2, 3],
  timeRanges: [{
  end: 720,
  start: 360
}, {
  end: 230,
  start: 160
}, {
  end: 230,
  start: 160
}],
  timezoneType: [{
  timezone_type: "AM/PM"
}, {
  timezone_type: "AM/PM"
}, {
  timezone_type: "AM/PM"
}]
}]
var expected = [];
array.forEach(a=>{
a.timeRanges.forEach((tr,index)=>{
expected.push({days:a.dayIds,startTime:tr.start,endTime:tr.end,timezone_type:a.timezoneType[index].timezone_type});
});
});

console.log(expected)

【问题讨论】:

    标签: javascript arrays expand


    【解决方案1】:

    使用reduce

    let arr = [{
      dayIds: [1],
      timeRanges: [{
          end: 1200,
          start: 800
        }],
      timezoneType: [{
      timezone_type: "AM/PM"
        }]
    }
    
    , {
      dayIds: [1, 2, 3],
      timeRanges: [{
      end: 720,
      start: 360
    }, {
      end: 230,
      start: 160
    }],
      timezoneType: [{
      timezone_type: "AM/PM"
    }, {
      timezone_type: "AM/PM"
    }, {
      timezone_type: "AM/PM"
    }]
    }]
    
    
    arr = arr.reduce((acc, cur) => {
        return [...acc, ...cur.timeRanges.map(timeRange => {
            return {dayIds:cur.dayIds, timezone_type:cur.timezoneType[0].timezone_type, endTime:timeRange.end, startTime:timeRange.start}
        })] 
    }, [])
    
    console.log(arr)

    【讨论】:

      【解决方案2】:

      使用flatMap 和解构。

      const convert = (arr) =>
        arr.flatMap(({ dayIds, timeRanges, timezoneType: [{ timezone_type }] }) =>
          timeRanges.map((range) => ({ ...range, dayIds: [...dayIds], timezone_type }))
        );
      const data = [
        {
          dayIds: [1],
          timeRanges: [
            {
              end: 1200,
              start: 800,
            },
          ],
          timezoneType: [
            {
              timezone_type: "AM/PM",
            },
          ],
        },
        {
          dayIds: [1, 2, 3],
          timeRanges: [
            {
              end: 720,
              start: 360,
            },
            {
              end: 230,
              start: 160,
            },
          ],
          timezoneType: [
            {
              timezone_type: "AM/PM",
            },
            {
              timezone_type: "AM/PM",
            },
            {
              timezone_type: "AM/PM",
            },
          ],
        },
      ];
      
      console.log(convert(data))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-29
        • 1970-01-01
        • 2017-07-05
        • 2020-03-11
        • 1970-01-01
        • 1970-01-01
        • 2014-03-17
        相关资源
        最近更新 更多