前端第二周,Javascript--数组的方法(会改变原数组的方法)

本文详细介绍了JavaScript中数组方法如push(),pop(),unshift(),shift(),reverse(),sort(),和splice()的功能、用法和示例,展示了如何在数组的开头或结尾添加、删除、替换元素,以及如何对数组进行排序和反转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.数组方法--添加和删除:

数组.push () 写

              作用:实现在数组末尾添加一个或多个成员

              返回值:数组添加成员元后 数组的新的长度

              是否会改变元素组:会

 举个栗子:

var arr1 = [true, 'hello', 123, 666, false, 999];

        console.log(arr1);

        var n = arr1.push('world');

        console.log(n);//7

        console.log(arr1);//[true, 'hello', 123, 666, false, 999, 'world']

        var b = arr1.push('11', 22, 33);

        console.log(b);  // 10

        console.log(arr1);//[true, 'hello', 123, 666, false, 999, 'world', '11', 22, 33]

 数组.pop() 不写

              作用:实现在数组的末尾删除一个成员

              返回值:数组被删除成员

              是否会改变元素组:会

  举个栗子:

var arr2 = [true, 'hello', 123, 666, false, 999];

        console.log(arr2);

        var c = arr2.pop();

        console.log(c);//999

        console.log(arr2);//[true, 'hello', 123, 666, false]

        var d = arr2.pop()

        console.log(d);//false

        console.log(arr2);// [true, 'hello', 123, 666]

数组.unshift()写

              作用:实现在数组的开头添加一个或多个成员

              返回值:数组添加成员数组的新的长度

              是否会改变原数组:会

 举个栗子:

  var arr3 = [true, 'hello', 123, 666, false, 999];

        console.log(arr3);

        var e = arr3.unshift('234');

        console.log(e);//7

        console.log(arr3);//['234', true, 'hello', 123, 666, false, 999]

        var f = arr3.unshift(1, 2, 3, 4, 5, 6, 7)

        console.log(f);//14

        console.log(arr3);// [1, 2, 3, 4, 5, 6, 7, '234', true, 'hello', 123, 666, false, 999]

 数组.shift()不写

             作用:实现在数组的开头删除一个成员

             返回值:数组被删除的成员

             是否会改变原数组:会

  举个栗子:

var arr4 = [true, 'hello', 123, 666, false, 999];

        console.log(arr4);

        var q = arr4.shift();

        console.log(q);//true

        console.log(arr4);//['hello', 123, 666, false, 999]

 数组.reverse() 不写  反转

             作用:实现将数组进行反转排序

             返回值:被反转排列后的新数组

             是否会改变原数组:会  

   举个栗子:

var arr1 = [true, 'hello', 123, 666, false, 999];

        console.log(arr1);

        var a = arr1.reverse();

        console.log(a);//[999, false, 666, 123, 'hello', true]

        console.log(arr1);//[999, false, 666, 123, 'hello', true]

 数组.sort() 不写   排序

                 作用:实现将数组中的元素按照字符串规则进行排序

                    默认按字符编码的顺序排列,非string类型会自动转换为string, 但是可以自定义排序规则。

                 返回值:排序后的新数组

                 是否会改变原数组:会

    举个栗子:

  var arr2 = [45, 28, 7, 100, 34, 86, 77];

        console.log(arr2);

        var b = arr2.sort();

        console.log(b);//[100, 28, 34, 45, 7, 77, 86] 有现象开头数字按小到大排序

        console.log(arr2);//[100, 28, 34, 45, 7, 77, 86]


 

        // 自定义排序规则:

        // 从小到大排序

        // sort方法传入一个函数作为实参

        // 函数有两个形参,函数内部return 第一个形参-第二个形参

        var arr3 = [99, 72, 61, 23, 12, 5, 69];

        console.log(arr3);

        var c = arr3.sort(function (a, b) {

            return a - b;

        });

        console.log(c);// [5, 12, 23, 61, 69, 72, 99]

        console.log(arr3);//[5, 12, 23, 61, 69, 72, 99]


 

        // 从大到小排序

        // sort方法传入一个函数作为实参

        // 函数有两个形参,函数内部return 第二个形参-第一个形参

        var d = arr3.sort(function (a, b) {

            return b - a;

        });

        console.log(d);// [99, 72, 69, 61, 23, 12, 5]

        console.log(d);// [99, 72, 69, 61, 23, 12, 5]

数组.splice() 写  删除(不保留)替换

               作用:通过指定的索引位置开始删除几个数组的元素,并且可以替换被删除的部分

                   第一个参数:指定开始删除数组的索引位置,包含此索引位置。

                   第二个参数:指定数组被删除的元素个数。

                   第三个参数以及后面的每个参数,是用来替换被删除位置的元素

                返回值:数组被删除部分组成新的新数组

                是否会改变原数组:会

     举个栗子:

var arr2 = ['hello', 123, 999, 'abc', true, 666, 'haha', false];

        console.log(arr2);

        //如果只传一个参数,表示从指定索引位置(包含)开始一直删除到数组结束

        var e = arr2.splice(4);

        console.log(e);// [true, 666, 'haha', false]

        console.log(arr2);//['hello', 123, 999, 'abc']

        var arr3 = ['hello', 123, 999, 'abc', true, 666, 'haha', false];

        console.log(arr3);

        //如果传入俩个参数,第一个参数指定的索引位置(包含)开始一直删除到第二个参数指定的个数。

        var f = arr1.splice(3, 4);

        console.log(f);//['abc', true, 666, 'haha']

        console.log(arr1);//['hello', 123, 999, false]

        var arr4 = ['hello', 123, 999, 'abc', true, 666, 'haha', false];

        console.log(arr4);

        var g = arr4.splice(2, 4, '弟弟', '妹妹');

        console.log(g);//[999, 'abc', true, 666]

        console.log(arr4);// ['hello', 123, '弟弟', '妹妹', 'haha', false]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值