• 数组的使用


    数组操作

    1. 对数组的最后一位进行操作
      • 添加
      • 书写格式:数组名.push(数据);

        例:let arr = ["A","B","C"];

        arr.push("D");

        console.log(arr); //["A","B","C","D"]

      • 删除
      • 书写格式:数组名.pop();

        例:let arr = ["A","B","C"];

        arr.pop();

        console.log(arr); //["A","B"]

    2. 对数组第一位进行操作
      • 添加
      • 书写格式:数组名.unshift(数据);

        例:let arr = ["A","B","C"];

        arr.push(1);

        console.log(arr); //[1,"A","B","C"]

      • 删除
      • 书写格式:数组名.shift();

        例:let arr = ["A","B","C"];

        arr.shift();

        console.log(arr); //["B","C"]

    3. pushunshift 和 popshift 的区别
      • 添加是可以,一次性添加多个数据;

      • 删除每次只能删除一个;

    4. 合并两个数组
      • 例: let arr = [1,2];

        let arr1 = [3,4];

        let arr2 = arr.concat(arr1);

        let arr3 = [...arr,...arr1];

        console.log(arr2); //[1,2,3,4]

        console.log(arr3); //[1,2,3,4]

    5. 截取 slice ()
      • 书写格式:数组名.slice(参数); //可以接收两个参数

        例:

        let arr = ["A","B","C","D","E","F"];

        // 情况一:一个参数(截取的开始下标);
        截取的内容是包括开始下标及以后的所有数据;

        let arr1 = arr.slice(2);

        console.log(arr1); //["C","D","E","F"]

        // 情况二:两个参数(截取的开始下标,截取的结束下标);
        截取的内容是开始下标(包括开始下标的内容) 到 结束下标(不包括结束下标的内容)的所有数据;

        let arr2 = arr.slice(2,4);

        console.log(arr2); //["C","D"]

        // 情况三:当不存在输入值的内容,则返回空数组 即 [ ]

        let arr3 = arr.slice(10);

        console.log(arr3); //[ ]

        // 情况四:当输入值为负值时,数组的长度加负值即下标,
        则截取的内容是开始下标(包括开始下标的内容) 到 结束下标(不包括结束下标的内容)的所有数据;

        let arr4 = arr.slice(-4,-2);

        console.log(arr3); //["C","D"]

    6. splice() 会改变原数组
      • 书写格式:数组名.splice(参数); //可以接收三个参数

        例:

        let arr = ["A","B","C","D","E","F"];

        //情况一: 两个参数
        功能:剪切
        第一个参数是起始下标,第二个为结束下标。(包含结束下标)

        let arr1 = arr.splice(2,5);

        console.log(arr1); //["C","D","E","F"]

        console.log(arr); //["A","F"]

        //情况二: 三个参数
        功能:插入修改(取决于第二个参数;第二个值为 0 时,代表插入;第二个值为 1 时,代表修改)

        let arr2 = arr.splice(1,0,5); //在下标为 1 的前面插入内容 5

        console.log(arr2); //["A",5,"B","C","D","E","F"]

        let arr3 = arr.splice(1,1,5); //将下标为 1 的数据改为内容 5

        console.log(arr2); //["A",5,"C","D","E","F"]

        //情况三: N个参数

        从第三个参数开始,以后的内容都是要插入的值

        let arr4 = arr.splice(1,0,5,6,7,8); //在下标为 1 的前面插入内容 5 6 7 8

        console.log(arr4); //["A",5,6,7,8,"B","C","D","E","F"]

    7. 一次性删除数组的内容

      let arr = ["A","B","C","D","E","F"];

      arr.length = 0;

      console.log(arr); //[ ]

    8. 数组转字符串 join()

      let arr = [1,2,3];

      let str = arr.join(",");

      console.log(str,typeof str); //1,2,3 string

    9. 字符串转数组

      let str = "Say hello to everybody!";

      let arr = str.split();

      console.log(arr); //[Say hello to everybody!]

      arr = str.split("");

      console.log(arr); //[S``a``y`` ``h``e``l``l``o`` ``t``o`` ``e``v``e``r``y``b``o``d``y``!]

      arr = str.split(" ");

      console.log(arr); //['Say','hello','to','everybody!']

    10. 遍历数组

      let arr = ["A","B","C","D","E","F"];

      //遍历输出数组中的每个值

      for(let item of arr){

      console.log(item);

      }

      //输出结果为 A B C D E F

      //遍历输出数组中的每个值的下标

      for(let item in arr){

      console.log(item);

      }

      //输出结果为:0 1 2 3 4 5

      //遍历输出数组中的每个值对应的键(下标)

      for(let item of arr.keys()){

      console.log(item);

      }

      //输出结果为:0 1 2 3 4 5

      //遍历输出数组中的每个值

      for(let item of arr.values()){

      console.log(item);

      }

      //输出结果为 A B C D E F

      //遍历输出数组中的每个值以及每个值对应的键(下标)

      for(let item of arr.entries()){

      console.log(item);

      }

      //输出结果为 [0,'A'] [1,'B'] [2,'C'] [3,'D'] [4,'E'] [5,'F']

    11. 判断标识符是不是数组

      let str = "123";

      let arr = ["A","B","C"];

      console.log(Array.isArray(str)); //false

      console.log(Array.isArray(arr)); //true

    12. 查找

      let arr = ["A","B","C","D","C"];

      //返回查找的值是否存在,存在则返回true,否则返回false

      arr.includes("C"); //true

      //从前往后找,找到第一个满足的值,返回其下标

      arr.indexOf("C"); //2

      //从后往前找,找到第一个满足的值,返回其下标

      arr.lastIndexOf("C");//4

      //如果没有找到,都会返回 -1

      arr.indexOf("E");arr.lastIndexOf(); //-1

    数组的使用

      通过下标或索引号获取到具体值。

      数组名[下标]

      注:下标(索引)的起始值是0

      例:

      let arr = ["A","B","C","D"];

      console.log(arr); //输出结果为 ["A","B","C","D"]

      console.log(arr[0]); //输出结果为 A

      console.log(arr[2]); //输出结果为 C

      console.log(arr[10]); //输出结果为 undefined

  • 相关阅读:
    UIViewcontroller生命周期方法
    runtime中的宏定义解释表
    opencv在Mac环境下的安装和配置
    Oracle导入导出
    Oracle触发器详解
    查找表的父表和子表
    Oracle创建表空间
    TRIGGER控制级联更新
    启动Oracle的bat文件
    PL(SQL)块
  • 原文地址:https://www.cnblogs.com/hg845740143/p/12180234.html
Copyright © 2020-2023  润新知