美文网首页
删除数组中多个指定对象

删除数组中多个指定对象

作者: my木子 | 来源:发表于2020-08-14 14:47 被阅读0次

用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)
          }
        }

相关文章

网友评论

      本文标题:删除数组中多个指定对象

      本文链接:https://www.haomeiwen.com/subject/wmvgdktx.html