如何把data数据形式转为newData数据形式的数据?

如何把data数据形式转为newData数据形式的数据?


const data = {
        1: [{
            category: '水果',
            time: 12,
        }, {
            category: '护肤品',
            time: 1,
        }, {
            category: '水果',
            time: 0.2,
        }, {
            category: '零食',
            time: 2,
        }, {
            category: '水果',
            time: 3,
        }, {
            category: '护肤品',
            time: 1,
        }, ],
        2: [{
            category: '水果',
            time: 2,
        }, {
            category: '零食',
            time: 2,
        }, {
            category: '水果',
            time: 2.3,
        }, ],
        3: [{
            category: '水果',
            time: 2.1,
        }, ],
        4: [],
        5: [],
        6: [],
    }
    const newData = {
        1: [{
            category: '水果',
            time: 12,
            x:0,
        }, {
            category: '水果',
            time: 0.2,
            x:12,
        }, {
            category: '水果',
            time: 3,
            x:12.2,
        }, {
            category: '护肤品',
            time: 1,
            x:12.5,
        }, {
            category: '护肤品',
            time: 1,
            x:12.6,
        }, {
            category: '零食',
            time: 2,
            x:12.7,
        }, ],
        2: [{
            category: '水果',
            time: 2,
            x:0,
        }, {
            category: '水果',
            time: 2.3,
            x:2,
        }, {
            category: '零食',
            time: 2,
            x:4.3,
        }, ],
        3: [{
            category: '水果',
            time: 2.1,
            x:0,
        }, ],
    }

示例代码如下


    const data = {
        1: [{
            category: '水果',
            time: 12,
        }, {
            category: '护肤品',
            time: 1,
        }, {
            category: '水果',
            time: 0.2,
        }, {
            category: '零食',
            time: 2,
        }, {
            category: '水果',
            time: 3,
        }, {
            category: '护肤品',
            time: 1,
        },],
        2: [{
            category: '水果',
            time: 2,
        }, {
            category: '零食',
            time: 2,
        }, {
            category: '水果',
            time: 2.3,
        },],
        3: [{
            category: '水果',
            time: 2.1,
        },],
        4: [],
        5: [],
        6: [],
    }
    const newData = {};
    for (let key in data) {
        if (data[key].length == 0) continue;
        newData[key] = [];
        let kv = { };
        for (let item of data[key]) {
            let x = kv[item.category] || 0;
            newData[key].push({ category: item.category, time: item.time, x: x });
            x += item.time;
            kv[item.category] = x;
        }
        //排序,将同类型放到一起
        let keys = Object.keys(kv); console.log(keys)
        newData[key].sort((a, b) => {
            let aIndex = keys.findIndex(x => x == a.category), bIndex = keys.findIndex(x => x == b.category);
            return aIndex - bIndex;
        })
    }
    console.log(newData)

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632