VUE对json数组中某个字符相同的数据添加到同个数组

2021-01-18 20:18

阅读:388

标签:直接   div   代码   char   json   color   style   nbsp   data   

直接上代码:

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 ) {
                    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)
            }
        }

 

VUE对json数组中某个字符相同的数据添加到同个数组

标签:直接   div   代码   char   json   color   style   nbsp   data   

原文地址:https://www.cnblogs.com/ren17/p/12912001.html


评论


亲,登录后才可以留言!