首页 > 编程语言 > 详细

js数组

时间:2019-05-26 00:44:24      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:key   div   分割   rip   doc   span   访问   world   lan   

1.一维数组

设置数组求和

技术分享图片
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
    var weight=[1.3,2.3,3,4,5,3.21321];
    var sum=0;
    //数组的遍历
    for(var i=0;i<weight.length;i++){
        sum+=weight[i];
        document.write(weight[i]+" ");
    }
    document.writeln("总体重是"+sum+" "+"平均体重"+(sum/weight.length)+"<br/>");
    document.write("体重保留为"+(sum/weight.length).toFixed(3));
        </script>
    </head>
    <body>
    </body>
</html>
View Code

数组的元素可以为任意元素,元素的值可以为任意的类型,

数组在内存中存在形式,数组为引用类型,

 2.数组细节

技术分享图片
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
            var weight = [1,2,3,4,5,6];
            var sum = 0;
            function abc(arr){
                weight[0]=9999;
            }
            //数组的遍历
            abc(weight);
            for(var i = 0; i < weight.length; i++) {
                document.write(weight[i] + " ");
            }
        </script>
    </head>

    <body>
    </body>

</html>
View Code

js中的数组为引用传递

 

3.数组的引用基本用法

越界的话会输出undefined

不能访问没定义的元素

技术分享图片
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
            var weight = [1, "hello", 2.5];
            window.alert(weight[16]);
        </script>
    </head>

    <body>
    </body>

</html>
View Code

 4.js的数组可以动态的增长

技术分享图片
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
            var weight = [1, "hello", 2.5];
            window.alert(weight.length);
            weight[3] = 12;
            window.alert(weight.length);
        </script>
    </head>

    <body>
    </body>

</html>
View Code

5.split函数的用法

技术分享图片
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
            var word = "hello world,大家好";
            var str = word.split(",");
            for(var i = 0; i < str.length; i++) {
                document.write(str[i] + "<br/>");
            }
        </script>
    </head>

    <body>
    </body>

</html>
View Code

对字符串进行分割

技术分享图片
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
            var word = "hello world,大家好";
            var str = word.split(",");
            for(var i = 0; i < str.length; i++) {
                document.write(str[i] + "<br/>");
            }
            var str1=word.split("");
            document.write("一个一个的分组"+"<br/>");
            for(var i = 0; i < str1.length; i++) {
                document.write(str1[i] + "&nbsp;");
            }
            str1=word.split(",",3);
            document.write(str[0]);
            document.write(str1.length);
        </script>
    </head>

    <body>
    </body>

</html>
View Code

 6.遍历数组的方式:var key in arr

技术分享图片
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script language="JavaScript" type="text/jscript">
            var word = "hello world,大家好";
            var str = word.split(",");
            for(var i = 0; i < str.length; i++) {
                document.write(str[i] + "<br/>");
            }
            var str1=word.split("");
            document.write("一个一个的分组"+"<br/>");
            for(var i = 0; i < str1.length; i++) {
                document.write(str1[i] + "&nbsp;");
            }
            str1=word.split(",",3);
            document.write(str[0]);
            document.write(str1.length);
        </script>
    </head>

    <body>
    </body>

</html>
View Code

 

js数组

标签:key   div   分割   rip   doc   span   访问   world   lan   

原文:https://www.cnblogs.com/helloworld2019/p/10920625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号