JS 数组试题训练

作者: 南极小丑 | 来源:发表于2019-07-22 15:27 被阅读0次

    Array 数组

    一些属性

    .push()//方法 往数组最后一位添加成员!
    .unshift()   // 往数组第一位添加成员!
    .pop()   //  将数组最后一位成员弹出!
    .shift ()  // 将数组第一位成员弹出!
    .join() // 将数组转换成字符串!
    .split()// 将字符串重新生成数组!括号里面写分隔符(自定义的);看字符串 
    .reverse() // 将数组成员倒序排列
    .concat  ()//将两个数组进行合并!
    .splice(1,2,3,4)//将数组中从下标为1的成员开始删除两个值 并在删除的位置添加3和4
    

    一些实例

    提取:http://www.jianshu.com?username=zhangshan&password=123456 该网址里面的 usernamepassword 部分。

    <script>
        
            var url1="http://www.jianshu.com?username=zhangsan&password=123456";
            var skr = url1.split("?"); //从“?”截取  获得新的数组有两个值
            console.log(skr)
            var sk2=skr[1];//获取下标为1的值
            var kkk=sk2.split("&");//在下标1的值,从“&”截取
            console.log(kkk)
            var t1=kkk[0];//获取kkk下标0的值
            var t2=kkk[1];//获取kkk下标1的值
            var user=t1.split("=");//从t1的“=”截取
            var pass=t2.split("=");//从t2的“=”截取
            document.write(user[1]);//渲染到页面user下标为1的值
            document.write("<br />")
            document.write(pass[1]);//渲染到页面pass下标为1的值
        var father = {
    </script>
    

    数组去重复!

    <script>
        
            var arr = ["a","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b","b","a","c","d","f","b"];
            var sum = [];
            var flag = true;
                for (var i in arr) {
                    flag = true;
                    for (var j in sum) {
                        if (arr[i] == sum[j]) {
                            flag = false;
                        }
                    } if (flag) {
                        sum.push(arr[i]);
                    }
    
                }
                document.write(sum);
        </script>
    

    var math=["语文 = 99","数学 = 90","英语 = 88","历史 = 25","化学 = 30","生物 = 80","体育 = 99","物理 = 100","政治 = 70"]; 求最大最小平均分总分!

    <script>
        var math=["语文=99","数学=90","英语=88","历史=25","化学=30","生物=80","体育=99","物理=100","政治=70"];
        var fenshu1 = [];
        var fenshu2 = [];
            for (var index in math) {
                    var fenshu1 = math[index].split("=");
                    fenshu2.push(parseInt(fenshu1[1]));
                }
                fenshu2.sort(daxiao);
    
                var sum = 0;
                for (var index in fenshu2) {
                    sum += fenshu2[index];
                }
     document.write("最低分为:" + fenshu2[0] + "<br />" + "最高分为:" + fenshu2[8] + "<br />" + "平均分为:" + sum / 9 + "<br />" + "总分为:" + sum)
    
    //函数大小定义
                function daxiao(a, b) {
                    if (a > b) {
                        return 1;
                    } else if (a == b) {
                        return 0;
                    } else if (a < b) {
                        return -1;
                    }
    
                }
        
        </script>
    

    相关文章

      网友评论

        本文标题:JS 数组试题训练

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