<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script> function compareTwo(jsonDate,key,listValue){ var arrPorts = listValue.split('/'); var prevValue = jsonDate.date.split('/'); var prevFirst = prevValue[0].substring(0,prevValue[0].length-1); var arrPortsFirst = arrPorts[0].substring(0,arrPorts[0].length-1); if(parseInt(key) == parseInt(jsonDate.index)+1 && prevFirst == arrPortsFirst && prevValue[1] == arrPorts[1] ){ return true; } return false; } function jsonLength(ports){ var length = 0; for(var key in ports){ length++; } return length; } /*获取字符串*/ function getPortStr(portList){ var prevDate = {index: '',date:''}; var length = 0; var equTmpStr = ''; var tmpStr = ''; var tmpLength = ''; var preStatus = false; var length = jsonLength(portList); alert("length:"+length); var ingStr = ''; for(var key in portList){ tmpLength++; var arrPorts = portList[key].split('/'); if('' != prevDate.index){ var comValue = compareTwo(prevDate,key,portList[key]); if(comValue){ //如果两个数是同类型的 //把现在的连接上以前用- equTmpStr = prevDate.date+'-'+arrPorts[2]; if(length == tmpLength){ ingStr += equTmpStr+','; equTmpStr = ''; } prevDate.index = key; continue; }else{ //如果是不同类型的,需要先存起来 alert("key:"+key+" comValue:"+comValue+" equTmpStr: "+equTmpStr); if('' != equTmpStr){ ingStr += equTmpStr+','; equTmpStr = ''; }else{ ingStr += prevDate.date+','; } if(length == tmpLength){ ingStr += portList[key]+','; } } } prevDate.index = key; prevDate.date = portList[key]; } ingStr = ingStr.substring(0,ingStr.length-1); return ingStr; } /*整理字符串和命令行显示一致*/ function getStrCollation(portList){ var strPort = getPortStr(portList); alert("strPort:"+strPort); var arrPort = strPort.split(','); var arrTmp = []; var prevStrPortPartFirst = ''; var prevStrPort = ''; var exportStr = ''; var j=0; for(var i=0;i<arrPort.length;i++){ var arrPortPart = arrPort[i].split('/'); var strPortPartFirst = arrPortPart[0].substring(0,arrPortPart[0].length-1); if(i){ if(prevStrPortPartFirst == strPortPartFirst){ arrTmp[j] = arrPort[i].substring(strPortPartFirst.length); j++; if(i == arrPort.length-1){ exportStr += arrTmp.join(','); } continue; }else{ if(arrTmp.length){ exportStr += arrTmp.join(',')+' '; arrTmp = []; j = 0; } if(i == arrPort.length-1){ exportStr += arrPort[i]; } } } arrTmp.push(arrPort[i]); j++; prevStrPortPartFirst = strPortPartFirst; } return exportStr; } /*使用这个函数的前提是json中的数据按照从小到大的顺序排好*/ function compareList(listSrc,listDes){ var srcLength = jsonLength(listSrc); var desLength = jsonLength(listDes); if(srcLength != desLength){ return false; }else{ for(var key in listSrc){ if(listSrc[key] != listDes[key]){ return false; } } } return true; } window.onload = function(){ var portList = {'1': 'gigaethernet1/1/1','2':'gigaethernet1/2/7','3':'gigaethernet1/2/3','7':'fst1/5/1','8':'fst1/5/2','10':'fst1/6/2','11':'fst1/6/3','12':'fst1/6/4','16':'fst1/9/4','18':'fst1/10/4'}; var portListTset = {'1': 'gigaethernet1/1/1','2':'gigaethernet3/1/7','3':'gigaethernet3/1/3','7':'fst1/5/1','8':'fst1/5/2'}; var arrPort = ['gigaethernet1/1/1','gigaethernet1/1/2']; var str = 'abcdefg'; //alert(str.substring(2)); alert(getStrCollation(portList)); }; </script> </head> <body> </body> </html>