赞
踩
var arr = [ { name: "ivr", time: "2020-01-01 12:10:09" }, { name: "ngbusi", time: "2020-12-02 05:04:12" }, { name: "message", time: "2020-04-06 10:12:30" }, { name: "ngwb", time: "2020-02-12 06:05:12" }, { name: "ivr", time: "2020-01-01 12:11:20" }, { name: "message", time: "2020-04-06 10:13:30" }, { name: "contact", time: "2020-03-12 16:05:50" } ]; var arrTrance = []; arr.sort(function(a, b) { return a.time > b.time ? 1: -1; }) // 同名节点合并 for (var i = 0; i < arr.length; i++) { if (arr[i].name == "ivr" || arr[i].name == "ngbusi" || arr[i].name == "message" || arr[i].name == "ngwb") { var dataArr = []; dataArr.push(arr[i]); // 嵌套循环arr[i]之后的数据,判断当前i和i+1,i+2等是否一致,一致就放入临时数组,并循环+1,否则不处理,跳出循环 for (var j = i + 1; j < arr.length; j++) { if (arr[i].name == arr[j].name) { dataArr.push(arr[j]); i++; } else { break; } } arrTrance.push({ name: arr[i].name, sameDatas: dataArr }) } else { arrTrance.push(arr[i]) } } // console.log(arrTrance); // [{name:"ivr", sameDatas:[{name:"ivr",time:"2020-01-01 12:10:09"},{name:"ivr",time:"2020-01-01 12:11:20"}]}]
// 链路精简,把时间在contact后面的节点放到contact的node节点里 var temp = []; for (var k = 0; k < arrTrance.length; k++) { if (arrTrance[k].name == "contact") { var moduleContact = arrTrance[k]; var sameData = []; // 嵌套循环判断后续节点,如非contact节点时,数据放入临时数组B.同时外层循环游标+1;如为人工节点时,跳出嵌套循环 for (var h = k + 1; h < arrTrance.length; h++) { if (arrTrance[h].name != "contact") { sameData.push(arrTrance[h]); k++ } else { break; } } // 将临时合并数组 放入原数组的合并节点 moduleContact.node = sameData; temp.push(moduleContact) } else { temp.push(arrTrance[k]); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。