效果图:
在这里插入图片描述
代码:

mounted() {
            this.CS();
        },
        methods: {
            CS() {
                var data = [{
                        "name": "C",
                        "size": 240,
                        "data": 57
                    },
                    {
                        "name": "F",
                        "size": 520,
                        "data": 42
                    },
                    {
                        "name": "G",
                        "size": 240,
                        "data": 47
                    },
                    {
                        "name": "C",
                        "size": 520,
                        "data": 23
                    },
                    {
                        "name": "F",
                        "size": 240,
                        "data": 14
                    },
                    {
                        "name": "G",
                        "size": 520,
                        "data": 62
                    },
                    {
                        "name": "C",
                        "size": 240,
                        "data": 34
                    },
                    {
                        "name": "F",
                        "size": 520,
                        "data": 25
                    },
                    {
                        "name": "G",
                        "size": 240,
                        "data": 35
                    }
                ]
                console.log(data)
                var echararr = [];
                for (var i = 0; i < data.length; i++) {
                    var arrindex=echararr.findIndex((item, index, arr) => {return item.name === data[i].name})//获取下标
                    if(arrindex==-1){//不存在盘符数据的添加盘符数据
                        echararr.push({
                            "name": data[i].name,
                            "size": data[i].size,
                            "data": [data[i].data]
                        });
                    }else{//有盘符则往盘符数据中添加
                        echararr[arrindex].data.push(data[i].data)
                    }
                    //console.log(arrindex);
                }
                console.log(echararr)
            }
        }```

Logo

前往低代码交流专区

更多推荐