02.数组的方法

99 阅读3分钟

一、数组的方法

1.push

  • 语法:数组.push(数据);
  • 作用:向数组末尾新增数据;
  • 返回值:追加数据后,数组最新长度;
    const arr = [ 1 , 2 , 3 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.push( 6 );
    console.log( res1 );
    console.log(arr);

运行结果:

Snipaste_2022-10-25_19-45-32.png

2.pop

  • 语法:数组.pop;
  • 作用:删除数组的最后一项;
  • 返回值:返回删除的数据;
    const arr = [ 1 , 2 , 3 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.pop( 5 );
    console.log( res1 );
    console.log(arr);

运行结果:

Snipaste_2022-10-25_19-49-57.png

3.unshift

  • 语法:数组.unshift();
  • 作用:向数据头部新增数据;
  • 返回值:返回新增后的数据长度;
    const arr = [ 1 , 2 , 3 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.unshift( 12 );
    console.log( res1 );
    console.log(arr);

运行结果:

Snipaste_2022-10-25_19-51-11.png

4.shift

  • 语法:数组.shift();
  • 作用:删除数组索引下标为0对应的数据数值;
  • 返回值:删除的数据数值;
    const arr = [ 1 , 2 , 3 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.shift( 1 );
    console.log( res1 );
    console.log(arr);

运行结果:

Snipaste_2022-10-25_19-53-07.png

5.sort

  • 语法:数组.sort(); 默认从小到大排序;
    • 数组.(function( a , b ){ return a - b }); 从小到大排序
    • 数组.( function( a , b ) { return b - a }) 从大到小排序
  • 作用:不传参数var num = arr.sort();
    • 会将数组内左右的值转化为字符串,然后一位一位的对比传参数,函数return a - b
    • 会将数组内所有的值,按照数字的从小到大排列,函数return b - a,会将数组内所有的值,按照数组的从大到小排列
  • 返回值:返回排列后的数组;
    const arr = [ 1 , 2 , 3 , 7 , 9 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.sort();
    console.log( res1 );

运行结果:

Snipaste_2022-10-25_19-55-11.png

6.splice

  • 语法:
    • var 变量 = 数组.splice( 参数1 , 参数2 , 参数3 );
    • 参数1:截取数组数据是指起始位置的索引下标;
    • 参数2:截取多少数据数值;
    • 参数3:新增数据数值 看实际情况定 是否添加还是不添加 可以有多个;
  • 作用:
    • 不传参数:截取数组中的某一段数据;
    • 传递参数:截取数组中的某一段数据,将第三个参数开始,当做新数据插入到数组内;
  • 返回值:截取后的数据(数组形式);
    const arr = [ 1 , 2 , 3 , 7 , 9 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.splice( 01 );
    console.log( res1 );

运行结果:

Snipaste_2022-10-25_19-57-17.png

7.slice

  • 语法: var 变量 = 数组.slice(开始位置索引 , 结束位置索引);
  • 作用: 截取数组中某一段数据数值;
  • 返回值:复制出来的内容;
  • 参数特点:
    • 新数组包含开始索引,不包含结束位置索引;
    • 参数接受赋值。
    const arr = [ 1 , 2 , 3 , 7 , 9 , 4 , 5 ];
    console.log( arr );
    let res1 = arr.slice( 01 );
    console.log( res1 );

运行结果:

Snipaste_2022-10-25_19-58-57.png

9.join

  • join将数组转化为字符串
  • 括号中写数组中数据数值对应的分隔符
let res2 = arr.join();
console.log(res2);

let res3 = arr.join('-');
console.log(res3);

10.indexOf

  • 查找这个数值在数组中是否存在
  • 如果存在返回这个数在数组中的索引下标
  • 如果不存在 返回-1

let res4 = arr.indexOf( 100 );
console.log(res4);

let res5 = arr.indexOf( '上海' );
console.log(res5);

console.log(arr);

11.lastIndexOf

  • 查找这个数据数值在数组中是否存在
  • 如果存在 返回这个数据数值在数组中最后出现的位置的索引下标
  • 如果不存在 返回-1

        const arr = [ 100 , 200 , function(){console.log( a + b )} ,  [ 1 , 2 , 3 ] ];
        console.log(arr);

        let res6 = arr.lastIndexOf( 123.123 );
        console.log( res6 );

        let res7 = arr.lastIndexOf( '北京' );
        console.log(res7);

        let res8 = arr.lastIndexOf( 100 );
        console.log(res8);

运行结果:

Snipaste_2022-10-25_20-03-05.png