js 從目標陣列中過濾掉 一個陣列元素,

人民的老藝術家發表於2020-07-25

 

標題描述的有點僵硬,大概需求是,從目標陣列中過濾掉我想要刪除的元素集合,這裡使用的是遍歷+過濾器的組合,很方便,做個筆記!

let old = ["AE_CN_SUPER_ECONOMY_G", "CAINIAO_EXPEDITED_ECONOMY", "CAINIAO_SUPER_ECONOMY_SG", "CAINIAO_SUPER_ECONOMY", "SINOTRANS_PY", "SUNYOU_ECONOMY", "YANWEN_ECONOMY", "SGP_OMP", "YANWEN_JYT", "SF_EPARCEL_OM", "CAINIAO_ECONOMY", "CAINIAO_ECONOMY_SG", "CAINIAO_STANDARD", "CAINIAO_STATION", "AE_CAINIAO_STANDARD", "CAINIAO_STANDARD_HEAVY", "CAINIAO_STANDARD_SG", "YANWEN_AM", "SGP", "CPAM", "EMS_ZX_ZX_US", "CAINIAO_PREMIUM"];
let del = ["SGP_OMP", "YANWEN_JYT", "SF_EPARCEL_OM", "CAINIAO_ECONOMY", "CAINIAO_ECONOMY_SG", "CAINIAO_STANDARD", "CAINIAO_STATION", "AE_CAINIAO_STANDARD"]

// let newList = [];//空容器
del.forEach((item)=>{
  //遍歷每個要刪除的元素  1
  old = old.filter((currentValue)=>{
     //定義一個過濾器,過濾器過濾掉有當前迴圈的值
      return currentValue !== item;
  })
  console.log("過濾過的新陣列",old)
})

 

相關文章