格式化双层数组使得时间复杂度为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))