• js 数组一些简单应用


    把两个数组连接成按从小到大的一个数组例如:

    var allowVlan = '23-25,45,4-6,67,50-53';

    var unTagVlan = '1-5';

    完成时应该是1-6,23-25,45,67,50-53;

    本例中用到了数组的方法:

    1.数组连接

    2.数组包含,

    3.数组排序

    4、数组去重

    备注:listCombinationStr 函数中包括字符串组成list 比如,[1,2,3,5,6,9] ===>>1-3,5-6,9这种形式,getStrCollationPort是强调了下这种形式

    <!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>
    
        window.onload = function(){
        //    var allowVlan = '23-25,45,4-6,67,50-53';
        //    var unTagVlan = '20-30';
            var allowVlan = '';
            var unTagVlan = '1-5';
            //重新排序
            
            var arrAllowVlan = vlanListSort(allowVlan);
            var arrUnTagVlan = vlanListSort(unTagVlan);
            
            alert("arrAllowVlan:"+arrAllowVlan+"arrUnTagVlan:"+arrUnTagVlan);
            //判断是不是包含关系
            if(includeRelation(arrAllowVlan,arrUnTagVlan)){
                /*是包含关系*/
                alert('1');
            }else{
                /*不是包含关系,需要重新组合*/
                alert('2');
                
                var tmpArr = arrAllowVlan.concat(arrUnTagVlan);
                alert("tmpArr:"+tmpArr);
                tmpArr.sort(function(a,b){
                    return a-b;
                });
                alert("result:"+listCombinationStr(tmpArr));
                }
            };
        
        /*判断两个数组是不是包含关系*/
        function includeRelation(arrParent,arrChild){
        
            if(arrChild.length > arrParent.length){
                return false;
            }
            for(var i=0;i< arrChild.length;i++){
                var bContent = true;    
                for(var j=0;j<arrParent.length;j++){
                    if(arrChild[i] == arrParent[j]){
                        bContent = false;
                        break;
                    }
                }
                if(bContent){
                    return false;
                }
            }
            return true;
        }
        
        /*排序*/
        function vlanListSort(str){
            var newArrAllow = [];
            var arrAllow = '';
            if(!str.length){
                return newArrAllow;
            } 
            arrAllow= str.split(',');
            for(var i=0;i<arrAllow.length;i++){
                if(arrAllow[i].indexOf('-') != -1){
                    var tmpArr = arrAllow[i].split('-');
                    for(var j=parseInt(tmpArr[0]);j<=parseInt(tmpArr[1]);j++){ //这里需要注意下'2'>'12';
                        newArrAllow.push(j);
                    }
                }else{
                    newArrAllow.push(parseInt(arrAllow[i]));
                }
            }
            newArrAllow.sort(function(a,b){
                return a-b;
            });
            
            return newArrAllow;
        }
        
        /*排好序的数组重新组合成新的列表,可能包含有重复的*/
        function listCombinationStr(tmpArr){    
                var outArr = [];
                var outStr = '';
                var tmpStr = '';
                
                tmpArr = deleteNumber(tmpArr);
                var tmpValue = tmpArr[0];
                for(var i=1;i<tmpArr.length;i++){
                    if(tmpArr[i] == (tmpArr[i-1]+1)){
                        tmpStr = tmpValue+'-'+tmpArr[i];
                        if(i == tmpArr.length-1){
                            outStr += tmpStr+',';
                        }
                    }else{
                        if(tmpStr != ''){
                            outStr += tmpStr+',';
                            tmpStr = '';
                        }else{
                            outStr += tmpValue+',';
                        }
                        
                        if(i == tmpArr.length-1){
                            outStr += tmpArr[i]+',';
                        }
                        tmpValue = tmpArr[i];
                    }
                }
                outStr = outStr.substring(0,outStr.length-1);
                return outStr;
            }
            
    
        /*排好序的数组去重*/
        function deleteNumber(tmpArr){
            var tmp = tmpArr[0];
            
            for(var i=1;i<tmpArr.length;i++){
                if(tmpArr[i] == tmp){
                    tmpArr.splice(i,1);
                    i--;    
                }else{
                    tmp = tmpArr[i];
                }
            }
            return tmpArr;
        }
        





    *之前写的是interface gigaethernet1/1/1 or fst1/10/4 这种形式的端口比如2128这种端口是port形式的故以前的不能用*/
    function getStrCollationPort(portList){
        
        /*var len = jsonLength(portList);
        var s1 = "port";
        var prevIndex = 0;
        var courrentIndex = 0;
        var arrPort = [];
        var tmpList = "";
        var tmpFirst = 0;
        var indexList = "";
    
        for(var i in portList){
             arrPort.push(portList[i].substring(s1.length));
        }
        alert("arrPort.length:"+arrPort.length);
        if(arrPort.length == 1){
            return arrPort[0].toString();
        }
        
        alert("arrPort"+arrPort);
        var prevIndex = arrPort[0];
        tmpFirst = prevIndex;
        for(var i=1;i<arrPort.length;i++){
            if(parseInt(prevIndex)+1 == parseInt(arrPort[i])){
                tmpList = tmpFirst+"-"+arrPort[i];
            }else{
                if(tmpList != ""){
                    
                    indexList += tmpList+',';
                    tmpList = "";
                    
                }else{
                    indexList += prevIndex+',';
                }
                //最后一个数
                if(i == arrPort.length-1){
                    indexList += arrPort[i];
                }
                tmpFirst = arrPort[i];
            }
            prevIndex = arrPort[i];
        }
        //最后一个数
        if(tmpList != ""){
            indexList += tmpList;
        }
        return indexList;
        */
                var outArr = [];
                var outStr = '';
                var tmpStr = '';
                var tmpArr = [];
                var s1 = "port";
                for(var i in portList){
                     tmpArr.push(portList[i].substring(s1.length));
                }
                alert("tmpArr:"+tmpArr);
                if(tmpArr.length == 1){
                    return tmpArr[0].toString();
                }
             
                var tmpValue = tmpArr[0];
                for(var i=1;i<tmpArr.length;i++){
                    if(parseInt(tmpArr[i]) == parseInt(tmpArr[i-1])+1){
                        tmpStr = tmpValue+'-'+tmpArr[i];
                        if(i == tmpArr.length-1){
                            outStr += tmpStr+',';
                        }
                    }else{
                        if(tmpStr != ''){
                            outStr += tmpStr+',';
                            tmpStr = '';
                        }else{
                            outStr += tmpValue+',';
                        }
                        
                        if(i == tmpArr.length-1){
                            outStr += tmpArr[i]+',';
                        }
                        tmpValue = tmpArr[i];
                    }
                }
                outStr = outStr.substring(0,outStr.length-1);
                return outStr;
           }
    
    
    

    </script> </head> <body> </body> </html>


    自己测试了没有什么问题,如果看到的人有什么新的思路,或者其他好的方法,欢迎来踩,一起讨论

  • 相关阅读:
    Linux平台不同解压缩命令的使用方法
    poj 1274 The Perfact Stall
    Experience Design for Sexable Forum
    JavaScript中的Array对象方法调用
    iOS9适配小结
    [Servlet&amp;JSP] HttpSession会话管理
    Android中的跨进程通信方法实例及特点分析(一):AIDL Service
    OpenCV——PS滤镜算法之Spherize 球面化(凸出效果)
    《Effective Modern C++》翻译--条款4:了解怎样查看推导出的类型
    Android开发系列之ListView
  • 原文地址:https://www.cnblogs.com/moon-yyl/p/9056889.html
Copyright © 2020-2023  润新知