删除数组中多个指定对象
2020-08-14 本文已影响0人
my木子
用for循环或者forEach遍历数组的话,在方法体内部splice都得不到正确的结果,解决方法是使用逆向循环
fileArr: [
{
select: false,
name: '图片1',
type: 'image',
url: 'https://api.cjbe88.com/storage/storage/d1d1202008131841228360.jpg'
},
{
select: true,
name: '视频1',
type: 'mp4',
url: 'https://api.cjbe88.com/storage/storage/d1d1202008131705152890.mp4'
},
{
select: true,
name: '图片2',
type: 'image',
url: 'https://api.cjbe88.com/storage/storage/d1d1202008131841228360.jpg'
},
{
select: false,
name: '视频3',
type: 'mp4',
url: 'https://api.cjbe88.com/storage/storage/d1d1202008131705152890.mp4'
}
];
for (let i = this.fileArr.length - 1; i >= 0; i--) {
if (this.fileArr[i].select) {
this.fileArr.splice(i, 1)
}
}