记一次复杂的数据转换

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

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值