JS-数组定义

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS-对象</title>
</head>

<body>

</body>
<script>
    //定义数组
    var arr = [1, 2, 3, 4, 5];
    console.log(arr[0]);
    console.log(arr[4]);

    //特点 长度可变 类型可变
    //默认值undefined
    var arr = [10];
    arr[10] = 100
    console.log(arr[10]);
    console.log(arr[9]);
    console.log(arr[8]);

    arr[6] = "hello";
    arr[7] = true;
    arr[11] = 'A';
    arr[1000] = true;
    console.log(arr[6]);
    console.log(arr[7]);
    console.log(arr);

</script>

</html>

for 循环遍历 遍历所有元素

但是forEach 空元素不遍历

//ES6 箭头函数

arr.forEach((e)=>{

console.log(e);

})

// 删除元素 下标指定

arr.splice(1,100)

// 添加元素

arr.push(7,8,9,10,11,"main")

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS-对象</title>
</head>

<body>

</body>
<script>
    //定义数组
    /* var arr = [1, 2, 3, 4, 5];
    console.log(arr[0]);
    console.log(arr[4]);

    //特点 长度可变 类型可变
    //默认值undefined
    var arr = [10];
    arr[10] = 100
    console.log(arr[10]);
    console.log(arr[9]);
    console.log(arr[8]);

    arr[6] = "hello";
    arr[7] = true;
    arr[11] = 'A';
    console.log(arr[6]);
    console.log(arr[7]);
    console.log(arr);

    for (let i = 0; i < arr.length; i++) {
       console.log(arr[i]) ;
        
    } */
    var arr=[]
   for(let i=0;i<=10;i++){

   
        arr[i]="hallo"
    }

    // for 循环遍历 遍历所有元素
    /* for (let i = 0; i < arr.length; i++) {
        console.log(arr[i]);
     } */

     //forEach 空元素不遍历
    arr.forEach(function(e){
        console.log(e);
    })
    
   //ES6 箭头函数
  /*  arr.forEach((e)=>{
    console.log(e);
   }) */
   
//    添加元素
   arr.push(7,8,9,10,11,"main")


 //ES6 箭头函数
 arr.forEach((e)=>{
    console.log(e);
   })

// 删除元素 下标指定
   arr.splice(1,100)
   console.log(arr);
</script>

</html>
相关推荐
NiceCloud喜云1 天前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
wordbaby1 天前
React Native + RNOH:跨页面数据回传的最佳实践与避坑指南
前端·react native
GISer_Jing1 天前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
丷丩1 天前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
AI玫瑰助手1 天前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车1 天前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋1 天前
C++14特性
开发语言·c++·c++14特性
Front思1 天前
AI前端工程师需要具备能力+
前端·人工智能·ai
JAVA社区1 天前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
弥树子1 天前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php