Vue-ElementUI:集合數組中刪除元素


問題舉例:shipmentNos: [],將某元素從shipmentNos中移除

方法一:使用forEach 和 splice

使用splice方法刪除下標為index的元素。
this.splice(index, 1); // 需要知道被移除元素的下標,若不知道,則需要遍歷集合,根據匹配條件找到該元素並獲取其下標

例:把 list = [0,1,2,3] 中的 3 移除。
    for(let i=0; i<list.length; i++){
      if(list[i] === 3){
        list.splice(i, 1);
        i--;
      }
    }

方法二:使用 filer

const list = listArr.filter(item => item.check)  
過濾 listArr中不要的元素,最終符合要求的元素賦值給list

例:removeShipmentNo() 方法,把傳入的對象 data的shipmentNo 從 this.shipmentNos 中移除。
removeShipmentNo(data) {
  const list = this.shipmentNos.filter(item => item !== data.shipmentNo)
  this.shipmentNos = list
}

本篇整理參考文章:
1.https://www.cnblogs.com/durenniu/p/10619675.html
2.https://blog.csdn.net/weixin_42230550/article/details/87990486


免責聲明!

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



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