美文网首页
关于js的零碎东西(3)

关于js的零碎东西(3)

作者: 战神七小姐 | 来源:发表于2018-10-07 13:43 被阅读0次
        添加值:.unshift()  .push()//返回的不是数值,是追加完成后的数组的长度
    
            .unshift()//在元素前面添加
    
                var a=["abc","as","df"];
    
                a.unshift(["dcfv"]);
    
                console.log(a);//["dcfv","abc","as","df"]
    
            .push()//在元素后面加
    
                var a=["abc","as","df"];
    
                a.push(["dcfv"]);
    
                console.log(a);//["abc","as","df","dcfv"]
    
        删除值:.shift()  .pop()  //返回值为删除的元素
    
            .shift()//在元素前面开始删
    
                var a=["abc","as","df"];
    
                console.log(a.shift("as"));//as
    
                console.log(a)//["as","df"]
    
            .pop()//在元素后面删
    
                var a=["abc","as","df"];
    
                console.log(a.pop("as"));//as
    
                console.log(a)//["abc","as"]
    
        数组的链接:.concat()
    
            var a=["abc","as","df"];
    
            var b=a.concat(["scd","dcf","rty"]);
    
            console.log(b);//["abc","as","df","scd","dcf","rty"]
    
        翻转值:reverse()
    
            var arr=[1,2,3,4];
    
            console.log(arr.reverse());//4,3,2,1
    
        数组排序:.sort()//返回的是排序后的数组
    
            var a=[1,30,40,5,80];
    
            console.log(a.sort());//1,30,40,5,80
    
            .sort()方法接收一个函数
    
                var a=[12,34,01,32];
    
                console.log(a.sort(function(a,b){
    
                    return a>b;//=a-b
    
                }))//01,12,32,34从小往大排//b>a==b-a从大往小排
    
        截数组:.slice() 和字符分.slice()的使用方式一样。区别:数组的返回值是截取出来的新数组,但对原值无影响
    
        var a="frgt";
    
        console.log(a.slice(1));//rgt
    
        console.log(a.slice(2));//gt
    
        splice:
    
            删除数组值:.splice(开始下标,长度),返回值:删除的数组
    
            添加数组值:.splice(开始下标,0,值1,值2...)返回值:[];
    
            替换数组值:.splice(开始下标,长度,值1,值2...)
    
        对象:arguments
    
            实参自动形成一个类数组,名字为arguments是一组实参的集合
    
            arguments.length 实参的个数
    
            arguments[0] 第一个实参
    
            arguments[arguments.length-1]最后一个实参下标从0开始
    
    10.函数
    
        1.封装indexOf()
    
            var arr=["a","b","c",123,456,"a","b"];
    
            function brr(n,str){
    
                for(var i=0;i<str.length;i++){
    
                    if(str[i]==n){
    
                        return i;
    
                        break;
    
                    }
    
                }
    
                return -9;
    
            }
    
            console.log(brr("a",arr));//0
    
        2.封装reverse()
    
            var arr=["a","b","c",123,456,"a","b"];
    
            function reverses(array){
    
              var brr=[];
    
              for(var i=0;i<array.length;i++){
    
                 brr.unshift(array[i]);
    
              }
    
              for(var i=array.length-1;i>=0;i--){
    
                brr.push(array[i]);
    
              }
    
              return brr;
    
            }
    
            console.log(reverses(arr));//["b","a",456,123,"c","b","a"]
    
        3.数组排重
    
            <1>.
    
                var arr=[1,2,3,3,4,5,4,6,6,7,8,9,]//[1,2,3,4,5,6,7,8,9]
    
                function abc(str){
    
                    var brr=[];
    
                    for(var i in str){
    
                        if(brr.indexOf(str[i]==-1)){
    
                            brr.push(str[i]);
    
                        }
    
                    }
    
                    return brr;
    
                }
    
                console.log(abc(arr));
    
        4.倒计时
    
            var hs=document.getElementById("hs");
    
            var year=document.getElementById("year");
    
            console.log(year.value);
    
            var timer=null;
    
            timer=setInterval(function(){
    
                var oDate=new Date();//当前日期
    
                var eDate=new Date("2018,5,27");//未来日期
    
                var cTime=eDate-oDate;//用未来日期减去现在日期 得到时间差
    
                //计算公式
    
                var day=24*60*60*1000;
    
                    hours=60*60*1000;
    
                    mines=60*1000;
    
                    secs=1000;
    
                var days=Math.floor(cTime/day),
    
                    hour=Math.floor(cTime%day/hours),
    
                    mine=Math.floor(cTime%hours/mine),
    
                    sec=Math.floor(cTime%mine/sec);
    
                    hs.innerHTML="距离考试还有"+day+"天"+hour+"时"+mine+"分"+sec+"秒";
    
            },1000)
    
        5.求平均值
    
            function arr(){
    
                var nums=0;
    
                for(var i=0;i<arguments.length;i++){
    
                    nums+=arguments[i];
    
                }
    
                console.log(nums/arguments.length);
    
            }
    
            arr(2,3,5,6);
    
        6.获取验证码
    
            <body>
    
                <h1 id="hh">saxx</h1>
    
                <button type="button" id="btn">点击获取验证码</button>
    
                <button type="button" id="btn1">换背景</button>
    
            </body>
    
            //获取验证码
    
            var btn=document.getElementById("btn");
    
            var hh=document.getElementById("hh");
    
            btn.onclick=function(){
    
                var str=[1,2,3,4,5,6,7,8,9,];
    
                var html="";
    
                for(var i=0;i<4;i++){
    
                    var strs;
    
                    strs=str[Math.floor(Math.random()*str.length)];
    
                    html+=strs
    
                }
    
                hh.innerHTML=html
    
            }//换背景
    
            var btn1=document.getElementById("btn1");
    
            btn1.onclick=function(){
    
                var arr=[1,2,3,4,5,6,7,8,9,0,"a","s","d","f","g","q","w","e","r"];
    
                var arrs="#";
    
                for(var i=0;i<6;i++){
    
                    var brr=arr[Math.floor(Math.random()*arr.length)];
    
                    arrs+=brr;
    
                }
    
                document.body.style.background=arrs;
    
            }
    

    相关文章

      网友评论

          本文标题:关于js的零碎东西(3)

          本文链接:https://www.haomeiwen.com/subject/ycjlaftx.html