需求:1、将data1的数据格式转换成data_result;2、反转

实现:

import percentageLine from "@/utils/percentageLine.js"

1、percentageLine(data1);

const snapLine = (data) => {
    if (Object.prototype.toString.call(data) !== '[object Array]') return;
     let all = [];
     if(data && data.length){
         for (let index = 0; index < data.length; index++) {
             const element = data[index];
             all.push(dueOne(element));
         }
     }
     dueTwo(all);
    return data;
}

function dueTwo(all) {
    const a1 = all[0];
    let arr = [];
    for (let index = 0; index < a1.length; index++) {
        const element = a1[index];
        let a2 = [];
        let obj = {
            name: element.name,
            data: element.data
        }
        a2.push(obj);
        for (let i = 1; i < all.length; i++) {
            let obj1 = {
                name: all[i][index].name,
                data: all[i][index].data
            }
            a2.push(obj1);
        }
        arr.push(a2);
    }
    return arr;
}

function dueOne(obj) {
    let _arr = [];
    for (let index = 0; index < obj.rateList.length; index++) {
        let aa = {
            name: obj.failedReason,
            data: obj.rateList[index]
        }
        _arr.push(aa);
    }
    return _arr;
}

export default snapLine;

实现:2、percentageLine(data_result);

const snapLine = (data) => {
    if (Object.prototype.toString.call(data) !== '[object Array]') return;
    let all_arr = [];
    if (data && data.length) {
        const _a1 = data[0];
        for (let index = 0; index < _a1.length; index++) {
            const element = _a1[index];
            let aa = [];
            for (let z = 0; z < _a1.length; z++) {
                aa.push(null);
            }
            let obj = {
                failedReason: element.name,
                rateList: aa
            }
            all_arr.push(obj);
        }

        for (let i = 0; i < all_arr.length; i++) {
            let single = all_arr[i].failedReason;
            for (let j = 0; j < data.length; j++) {
                for (let m = 0; m < data[i].length; m++) {
                    if (single === data[i][m].name) {
                        all_arr[i].rateList[j] = data[j][m].data
                    }
                }
            }
        }
        
    }

    return data;
}
export default snapLine;

 

数据结构展示:

const data1 = [
    {
        "rateList": [1,2,3,4,5,6,7,8,9,10,11,12],
        "failedReason":"苹果"
    },
    {
        "rateList": [10,20,30,40,50,60,70,80,90,100,110,120],
        "failedReason":"香蕉"
    },
    {
        "rateList": [100,200,300,400,500,600,700,800,900,1000,1100,1200],
        "failedReason":"西瓜"
    }
];

const data_result = [
    [
        {name: "苹果", data: 1},
        {name: "香蕉", data: 10},
        {name: "西瓜", data: 100}       
    ],
    [
        {name: "苹果", data: 2},
        {name: "香蕉", data: 20},
        {name: "西瓜", data: 200}       
    ],
    [
        {name: "苹果", data: 3},
        {name: "香蕉", data: 30},
        {name: "西瓜", data: 300}
    ],
    [
        {name: "苹果", data: 4},
        {name: "香蕉", data: 40},
        {name: "西瓜", data: 400}       
    ],
    [
        {name: "苹果", data: 5},
        {name: "香蕉", data: 50},
        {name: "西瓜", data: 500}       
    ],
    [
        {name: "苹果", data: 6},
        {name: "香蕉", data: 60},
        {name: "西瓜", data: 600}
    ],
    [
        {name: "苹果", data: 7},
        {name: "香蕉", data: 70},
        {name: "西瓜", data: 700}       
    ],
    [
        {name: "苹果", data: 8},
        {name: "香蕉", data: 80},
        {name: "西瓜", data: 800}       
    ],
    [
        {name: "苹果", data: 9},
        {name: "香蕉", data: 90},
        {name: "西瓜", data: 900}
    ],
    [
        {name: "苹果", data: 10},
        {name: "香蕉", data: 100},
        {name: "西瓜", data: 1000}       
    ],
    [
        {name: "苹果", data: 11},
        {name: "香蕉", data: 110},
        {name: "西瓜", data: 1100}       
    ],
    [
        {name: "苹果", data: 12},
        {name: "香蕉", data: 120},
        {name: "西瓜", data: 1200}
    ]
];

 

posted on 2021-01-11 14:08  羽丫头不乖  阅读(114)  评论(0编辑  收藏  举报