選中取消push和splice刪除

hql丶1024發表於2020-06-24
data:{
arry:[
    {
      id:1,
      name:'dada'
    },
    {
      id:2,
      name:'aaa'
    }
  ]
}
 fanxuan(){
    let that = this
    let arry = that.data.arry
    let id = '4'//當前選中的id
    let aaa = {
      id:3,
      name:'woshi3'
    }//當前選中的物件
    for(let i in arry){
      if(arry[i].id == id){//判斷當前push的有沒有重複的,有就反選
        arry.splice(i,1)
        console.log(arry)
        return false
      }
    }
    arry.push(aaa)
    console.log(arry)
  },

相關文章