js 格式化双层数组使得时间复杂度为O(n)的尝试

格式化双层数组使得时间复杂度为O(n)的尝试

  1. 提取关键数据,将双层数组扁平化为单层
  2. 单层再做转换
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))
上一篇:# nodeJs初探及简单的前后台交互


下一篇:cnpm : 无法加载文件 D:\ProgramFiles\nodejs\cnpm.ps1,因为在此系统上禁止运行脚本