数组方法(可以改变原数组)

可以改变原数组的数组方法有:

                push--尾部添加

                pop--尾部删除

                shift--头部删除

                unshift--头部添加

                sort--排序

                splice--添加、删除、替换     本质是删除

 var arr=[

        {name:1,checked:false},

        {name:2,checked:true},

        {name:3,checked:true},

        {name:4,checked:false},

        {name:5,checked:false},

        {name:6,checked:true},

        ]

一 、push--尾部添加

        this.arr.push({

            name:7,

            checked:false

        })

        console.log(this.arr);

        

二、 pop-- 尾部删除 

        this.arr.pop()

        console.log(this.arr); 

        

三、 shift--头部删除

        this.arr.shift()

        console.log(this.arr);  

        

四、  unshift--头部添加

        this.arr.unshift({

            name:0,

            checked:true

        })

        console.log(this.arr);

        

 五、reverse--数组翻转

        this.arr.reverse()

        console.log(this.arr);

        

        

六、 sort--排序

        var num=[1,5,13,8,9,4,6,7]

        this.num.sort((a,b)=>a-b)

        console.log(num);

        this.num.sort((a,b)=>b-a)

        console.log(num);

        

七、splice--添加、删除、替换 

        this.num.splice(0,1,3)  //在下标为0的地方删除一项,添加一个3

        console.log(num);

        this.num.splice(0,1,3,5,6) //在下标为0的地方删除一项,添加一个3,5,6

        console.log(num);

        this.num.splice(5,1)   //在下表为5的地方删除1项

        console.log(num);

        

        

猜你喜欢

转载自blog.csdn.net/weixin_55521186/article/details/126347487