JS——对象:数组

数组的定义

/* 数组定义方法1 */
var name_1 = new Array(a,b,c,..)
/* 数组定义方法2 */
var name_2 = [m,n,t,...]

案例 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /* 数组定义方法1 */
        var aa = new Array(1,2,3)

        /* 数组定义方法2 */
        var bb = [11,22,33]

        console.log(aa);
        console.log(bb);

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

特点:长度和类型都可变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var aa = new Array(1,2,3)

        /* 数组长度是可变的 */
        aa[10] = 10;
        console.log(aa);

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var aa = new Array(1,2,3)

        /* 数组类型是可变的 */
        aa[6]="A";
        aa[8]=true;
        console.log(aa);

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

属性和方法 

arr.length;
arr.forEach(function(name){ 运行代码});
arr.forEach((name)=>{运行代码});
arr.push(1,"字符串");
arr.splice(下标,所删减的数量);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [0,1,2,3,4]
        /* 获取数组长度 */
        console.log(arr.length);

        console.log("---------------------------------");
        /* 遍历数组有值元素方法1 */
        arr.forEach(function(e){
            console.log(e);
        })

        console.log("---------------------------------");
        /* 遍历数组有值元素方法1 */
        arr.forEach((r) => {
            console.log(r);
        })

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

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [0,1,2,3,4]

        /* 添加元素 */
        arr.push(7,8,9,'a','d');
        console.log(arr);
        console.log("---------------------------------");
        /* 删除元素 */
        arr.splice(0,3)
        console.log(arr);
        console.log("---------------------------------");

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

 

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值