zoukankan      html  css  js  c++  java
  • 记一次复杂的数据转换

    接到这么一个需求,后端什么都已经做好了才通知前端有这么一个任务,所以接口返回的数据结构无法更改,所以才有了以下的事情。

    let oldList = [
        [
            "test-bt-api-gateway_test-gw-dataupload-api_test-srm-operation-api"
        ],
        [
            "test-bt-api-gateway_test-message-sms-service_test-bt-app-api_test-bt-cpsp-account-service_test-bt-mall-goodsmanage-service"
        ],
        [
            "test-bt-api-gateway_test-front-api_xiandou-test-bt-zt-gateway_test-user-auth-service_test-user-account-service"
        ],
        [
            "test-bt-api-gateway_test-srm-api_xiandou-test-bt-zt-gateway_test-srm-vehicle-service"
        ]
    ]
    let resData = {
        nodes: [
            { id: '1', name: 'test-bt-api-gateway' },
            { id: '2', name: 'test-gw-dataupload-api' },
            { id: '3', name: 'test-srm-operation-api' },

            { id: '4', name: 'test-message-sms-service', },
            { id: '5', name: 'test-bt-app-api' },
            { id: '6', name: 'test-bt-cpsp-account-service' },
            { id: '7', name: 'test-bt-mall-goodsmanage-service' },

            { id: '8', name: 'test-front-api' },
            { id: '9', name: 'xiandou-test-bt-zt-gateway' },
            { id: '10', name: 'test-user-auth-service' },
            { id: '11', name: 'test-user-account-service' },

            { id: '12', name: 'test-srm-api' },
            { id: '13', name: 'test-srm-vehicle-service' },
        ],
        calls: [
            { id: '1_2', source: '1', target: '2', value: 0 },
            { id: '2_3', source: '2', target: '3', value: 0 },
            { id: '1_4', source: '1', target: '4', value: 1 },
            { id: '4_5', source: '4', target: '5', value: 1 },
            { id: '5_6', source: '5', target: '6', value: 1 },
            { id: '6_7', source: '6', target: '7', value: 1 },
            { id: '1_8', source: '1', target: '8', value: 2 },
            { id: '8_9', source: '8', target: '9', value: 2 },
            { id: '9_10', source: '9', target: '10', value: 2 },
            { id: '10_11', source: '10', target: '11', value: 2 },
            { id: '1_12', source: '1', target: '12', value: 3 },
            { id: '12_9', source: '12', target: '9', value: 3 },
            { id: '9_13', source: '9', target: '13', value: 3 },
        ],
    };

    需要把oldList 数组转化成resData 这种结构,才能进行后续的任务。

    oldList 中的每一项中,以下划线_为分割线,第二个依赖于第一个,第三个依赖于第二个,以此类推;

    所以生成的id要反映出依赖关系,根依赖服务id为1

    以下为实现逻辑

    let newList = [],
        nodes = []
    new Promise(resolve => {
        //处理原始数据,将原始数据中,每一项以_为分割点,切割成数组
        oldList.map((item, i) => {
            let tempList = item[0].split("_");
            tempList.map((tempItem, j) => {
                let obj = {
                    name: tempItem,
                    id: j + 1,
                    value: i
                }
                newList.push(obj)
            })
        })
        //去重
        nodes = uniqueArray(JSON.parse(JSON.stringify(newList)), "name");
        //给node数组添加id,得到需要的node数组
        nodes.map((item, i) => {
            item.id = i + 1
        })
        resolve()
    }).then(res => {
        let tempList = [],
            parentName = newList[0].name;
        new Promise(resolve => {
            newList.map(item => {
                tempList.push(item)
            })
            for (let i = 0; i < newList.length - oldList.length; i++) {
                tempList[i].id = i + 1;
            }
            resolve()
        }).then((res) => {
            nodes.map(item => {
                tempList.map((item2, index) => {
                    if (item.name === item2.name) {
                        item2.id = item.id
                    }
                })
            })
            let callsList = [];
            tempList.map((item, index) => {
                if (index !== tempList.length - 1) {
                    if (tempList[index + 1].id !== 1) {
                        let obj = {
                            source: item.id,
                            target: tempList[index + 1].id,
                            value: item.value,
                            id: item.id + "_" + tempList[index + 1].id
                        }
                        callsList.push(obj);
                    }
                }
            })
        })
    })
    //数组根据name去重
    function uniqueArray(array, key) {
        var result = [array[0]];
        for (var i = 1; i < array.length; i++) {
            var item = array[i];
            var repeat = false;
            for (var j = 0; j < result.length; j++) {
                if (item[key] == result[j][key]) {
                    repeat = true;
                    break;
                }
            }
            if (!repeat) {
                result.push(item);
            }
        }
        return result;
    }
  • 相关阅读:
    Spring.Net + Ibatis.Net + Log4Net 结合搭的一个简单多层开发架子
    驳网上常见的半调子程序员白痴言论
    IT对联大汇总
    ClassBase A = New ClassSon() 全面分析
    C# 反射/映射学习
    C++ 一般类型与变量
    HttpModule是如何工作的(转自大牛)
    SQL Server 中的嵌套事务与@@TranCount(转)
    IT人不要一直做技术(转我也不知道转了几圈了)
    kingcms的双重循环
  • 原文地址:https://www.cnblogs.com/yixiancheng/p/12459062.html
Copyright © 2011-2022 走看看