Map去重

wrapData = (array = []) => {
        const map = new Map();
        for (let index = 0; index < array.length; index++) {
            const element = array[index];
            const { category } = element;//
            let fix = [];
            if (map.has(category))
                fix = map.get(category);
            fix.push(element);
            map.set(category, fix);
        }
        const fix = [...map.values()].map(v => {
            let _type = v[0].category;
            let _percent = 0;
            let _value = 0;
            for (let index = 0; index < v.length; index++) {
                const element = v[index];
                _percent += element.percent;
                _value += element.y;
            }
            return {
                x: _type,
                category: _type,
                percent: _percent,
                y: _value
            };
        });
        return fix;
    }

  

posted @ 2022-05-10 22:41  吃鸡小能手  阅读(135)  评论(0编辑  收藏  举报