vue學習(十三) 刪除對象數組中的某個元素


//html
<div id="app">
  //v-for循環就不寫了 每一條數據最后都有一個刪除的超鏈 .prevent阻止默認的跳轉行為 只執行點擊事件
  <a href="" @click.prevent="del(item.id)">刪除</a> </div> //script <script>   var vm = new Vue({     el:'app',     data:{       id:'',
      name:'',
      list:[
        {id:1, name:'驚鯢'},
        {id:2, name:'掩日'},
        {id:2, name:'黑白玄翦'}
      ]     },     methods:{
//methods中定義了當前vue實例中所有可用的方法       del(id){
        this.list.some((item, i)=>{
          if(item.id==id){
            this.list.splice(i, 1)
            //在數組的some方法中,如果return true,就會立即終止這個數組的后續循環
            return true
          }
        })      
      },
      del(id){
        var index = this.list.findIndex(item =>{
          if(item.id==id){
            return true
          }
        })
        this.list.splice(index,1)
      }
    }   }) </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM