zoukankan      html  css  js  c++  java
  • 查询、移除数组中特定值

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="jquery-3.3.1.min.js"></script>
    </head>
    <body>
        <div id="result"></div>
    </body>
    <script>
        $(function(){
            var data = "q,w,e,r,d,f,a,1,2,3";
            var dataArr = data.split(",");
            var dataArr_Ascii = dataArr.concat();//复制数组
            console.log(dataArr);
            var idx = [];
            //也可以用正则表达式,这里用的是ASCII码
            var newArr = $.each(dataArr_Ascii,function(key,val){
                dataArr_Ascii[key] = dataArr_Ascii[key].charCodeAt();
            });
            console.log(newArr,dataArr_Ascii,dataArr);
            //48~57为0到9十个阿拉伯数字
            //注意 仅适用于0~9的情况
            //多位数字就得用正则把数字取出来了
            $("#result").append("数据中是数字的有:");
            $.grep(newArr,function(val,key){
                if(val < 57 && val > 48 ){
                    var fCCVal = String.fromCharCode(val);
                    $("#result").append(fCCVal + ",");
                }
                // console.log(String.fromCharCode(val));
            });
    
            $("#result").append("前五位:");
            var front5 = $.grep(dataArr,function(val,key){
                return key > 4;
            },true);//表示反向结果,下标的值小于5
    
            $.each(front5,function(key,val){
                $("#result").append(val + ",");
            });
    
            $.grep(dataArr_Ascii,function(val,key){
                console.log(dataArr_Ascii);
                if(val < 57 && val > 48 ){
                    idx.push(String.fromCharCode(val));
                }
            });
    
            console.log(idx);
            $.each(idx,function(key,val){
                dataArr_Ascii.splice($.inArray(val,dataArr_Ascii),1);//$.inArray()获取数组值的下标,再用splice()将其从数组中删除
            });
            console.log(dataArr_Ascii);
    
    
        })
    </script>
    </html>
  • 相关阅读:
    PAT (Basic Level) Practise:1001. 害死人不偿命的(3n+1)猜想
    流加密法
    The NMEA 0183 Protocol
    USB 描述符
    网摘
    What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?
    Why did Jimmy Wales invest in Quora? Is he afraid that it will take over Wikipedia?
    Add Binary
    Cocos2d-x 网络资源
    Cache
  • 原文地址:https://www.cnblogs.com/linbudu/p/10910109.html
Copyright © 2011-2022 走看看