问题举例: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