ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

记一次复杂的数据转换

2020-03-10 22:00:13  阅读:184  来源: 互联网

标签:转换 name 复杂 value source api test 数据 id


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

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

 

标签:转换,name,复杂,value,source,api,test,数据,id
来源: https://www.cnblogs.com/yixiancheng/p/12459062.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有