8. 数据分类

在遇到数据需要根据标题等进行分类:

//数据
let data = [
    {
        device_id: "AAAAAAA",
        filename: "Log_2019-04-09-00-00.tar",
    },
    {
        device_id: "AAAAAAA",
        filename: "Log_2019-04-09-00-01.tar",
    },
    {
        device_id: "BBBBBBB",
        filename: "Log_2019-04-09-00-02.tar",
    },
];

//分类
export function classifyLogList(data) {
    let newArr = [];
    data.map((oldData, i) => {
        let index = -1;
        let alreadyExists = newArr.some((newData, j) => {
            if (oldData.device_id === newData.device_id) {
                index = j;
                return true
            }
        })
        if (alreadyExists) {
            newArr[index].file_list.push(oldData.filename)
        } else {
            newArr.push({
                device_id: oldData.device_id,
                file_list: [oldData.filename],
            })
        }
    })
    return newArr
}


classifyLogList(data);
//结果
[
    {
        device_id: "AAAAAAA",
        file_list: ["Log_2019-04-09-00-00.tar","Log_2019-04-09-00-01.tar"],
    },
    {
        device_id: "BBBBBBB",
        file_list: ["Log_2019-04-09-00-02.tar"],
    },
]

 

posted @ 2019-04-09 17:08  大耳朵小虎  阅读(122)  评论(0编辑  收藏  举报