- 提取外层关键数据(如果需要),将双层数组扁平化为单层
- 单层再做转换,很容易得到O(n)的实现
const infos = [
{
time: '2022-02-21',
data: [{
Duration: 22,
Spec: "h264"
},
{
Duration: 33,
Spec: "h265_hd"
},
{
Duration: 44,
Spec: "h264_4k"
}]
},
{
time: '2022-02-22',
data: [{
Duration: 55,
Spec: "h264"
},
{
Duration: 66,
Spec: "h265_hd"
},
{
Duration: 77,
Spec: "h264_4k"
}]
}
]
const _map = list => list
.reduce((prev, cur) => prev.data.concat(cur.data))
.reduce((total, { Duration, Spec }) => Object.assign(total, { [Spec]: (total[Spec] || []).concat(Duration) }), {})
const _list = map => Object.entries(map).map(([name, data]) => ({ name, data }))
_list(_map(infos))