js根据索引删除数组批量指定元素

 // 批量删除
            batchdel(){
                let ids = [];
                if(this.checkArray.length >= 1) {
                    this.checkArray.forEach((item1, index1) => {
                        this.ticketingProceedList.forEach((item2, index2) => {
                            if (item1){
                                if (index1 == index2) {
                                    ids.push(item2.id);
                                }
                            }
                        });
                    });

                    if (ids.length<=0){
                        this.$mui.toast('暂无选中数据',{ duration:1000, type:'div' });
                        return false;
                    }
                    vm.$mui.confirm('确定要删除吗?','提示',['取消','确定'],(e)=>{
                        if(e.index==1){
                            this.$ajax.del('/ticketing/delByIds/'+ids.join(',')).then(res =>{
                                if (res.success){
                                    this.$mui.toast('操作成功',{ duration:1000, type:'div' });
                                    for (let i = this.checkArray.length -1;i>=0;i--){
                                        if (this.checkArray[i]){
                                            this.appearTrainList.splice(i,1);
                                        }
                                    }
                                    this.checkArray = [];
                                }else {
                                    this.$mui.toast('操作失败',{ duration:1000, type:'div' });
                                }
                            });
                        }
                    },'div');

                }else {
                    this.$mui.toast('没有可删除的选项',{ duration:1000, type:'div' });
                }
            },

注意:采用foreach不能执行最后一个元素,所以当你用splice批量删除元素时采用for循环方式即可解决批量删除问题!!!!

发布了17 篇原创文章 · 获赞 0 · 访问量 207

猜你喜欢

转载自blog.csdn.net/qq_41444226/article/details/103269199
今日推荐