js数组

65 阅读1分钟

for练习

<script>
    //5行5列*
        //行数
        for (let index = 1; index <= 5 ; index++) {
            //列数
           for (let index2 = 1; index2 <= 5; index2++) {
               //输出的文字
              document.write('*')
           }
           //行数同级,输出换行
            document.write('<br>')
        }
    </script>
 <script>
        //倒三角5行星星
       
       for (let index = 1; index <=5; index++) {
          for (let index1 = 1; index1 <= index; index1++) {
              document.write('*')
          }
          document.write('<br>')
       }
    </script>

<script>
     //99乘法表
for (let index = 1; index <= 9; index++) {
        for (let index1 = 1; index1 <= index; index1++) {
          let num = index1 * index;
          
          document.write(`<span>  ${index1} * ${index} = ${num}     </span>`);
        }
        document.write('<br/>');
      }
    </script>

万少语录

 碰到 不会做的案例的时候 要怎么办
      1 百度(最终的解决方案)

      2 找和以前旧案例的联系!!!   尽可能去找!!! 
        (相等于每一个案例对你来是,都是全新,没有得到 每一个案例的开发经验!)

      4 拆解
      5  分析 主次功能

      分析:
      1 比较主要的是里面的文字 该是让显示 
        (要从行来开  不要从列来看 )
 需求分析:(用文字 把看见的需求 描述下来)

1648686692266.png

数组

数组(Array)是一种可以按顺序保存数据的数据类型

数组的基本语法

1648650364713.png

1648650544332.png

1648650603412.png

for数组搭配使用

<script>
        let arr = ["大喵","二喵","三喵","四喵","五喵"]
        for (let index = 0; index < arr.length; index++) {
           console.log(arr[index]);
        }
    </script>

1648650758203.png

操作数组

数组本质是数据集合, 操作数据无非就是 增 删 改 查 语法

查:询数组数据

数组[下标] 或者我们称为访问数组数据

改: 重新赋值

数组[下标] = 新值

增: 数组添加新的数据

数组结尾增加: arr.push(新增的内容)

1648651057964.png

 <script>
        let arr = ['苹果','香蕉','橙子']
        arr.push('西瓜')
        console.log(arr);
    </script>

数组开头增加: arr.unshift(新增的内容)

1648651219587.png

删:除数组中数据

删除数组结尾 arr.pop()

pop去掉最后一个,并返回最后一个名称

1648651822946.png

 <script>
        let arr = ['苹果','香蕉','橙子']
       
        //pop去掉最后一个,并返回最后一个名称
        let last = arr.pop()
        console.log(last);
        console.log(arr);
    </script>

删除数组开头arr.shift()

first去掉第一个,并返回第一个名称

1648651844136.png

 <script>
        let arr = ['苹果','香蕉','橙子']
        //first去掉第一个,并返回第一个名称
        let first = arr.shift()
         console.log(first);
       
        console.log(arr);
    </script>

删除任意数组位置的名称 arr.splice(操作的下标,删除的个数)

1648651868439.png

  <script>
         let arr = ['苹果','香蕉','橙子']
      //指定位置删除数组,第一个数写位置,第二个数写删除的个数,可以返回删除的数据
     let last = arr.splice(1,2)
        console.log(last);
        console.log(arr);
    </script>
        <script>
         let arr = ['苹果','香蕉','橙子']
    //第二个位置写0,可以不删除,然后写内容,在指定位置添加数组内容
       let last = arr.splice(1,0,'梨')
        console.log(last);
        console.log(arr);
    </script>

数组补充

.length数组长度

1648652887062.png

Snipaste_2022-03-31_08-23-56.png