如何过滤照片名称数组?

How do I filter an Array of photo names?

我有一个包含 UUID 的图像名称数组。

图片

[
 "auditid_626_UUID_666666_time__1582577405550.jpg",
 "auditid_626_UUID_999999_time__1582577405554.jpg",
 "auditid_626_UUID_999999_time__1582577405557.jpg"
]

我在 html 页面中显示它们

<ion-list>
  <ion-item *ngFor="let img of images; index as pos" class="ion-text-wrap">
    <ion-thumbnail slot="start">
      <ion-img [src]="img.path"></ion-img>
    </ion-thumbnail>

    <ion-textarea auto-grow="true" >{{ img.name }}</ion-textarea>
    <ion-button slot="end" fill="clear" (click)="startUpload(img)">
        <ion-icon slot="icon-only" src="assets/upload.svg"></ion-icon>
      </ion-button>
    <ion-button slot="end" fill="clear" (click)="deleteImage(img, pos)">
      <ion-icon slot="icon-only" name="trash"></ion-icon>
    </ion-button>
  </ion-item>
</ion-list>

我调用这个数组的方法是

ngOnInit() {
  this.testID = "666666";
}

loadStoredImages() {
  this.storage.get(this.STORAGE_KEY).then(images => {
    if (images) {
      let arr = JSON.parse(images);
      this.images = [];  // filter here  .filter((images: any[]) => { return images.includes(this.testID) } );
      for (let img of arr) {
        let filePath = this.file.dataDirectory + img;
        let resPath = this.pathForImage(filePath);
        this.images.push({ name: img, path: resPath, filePath: filePath });
      }
    }
  });
}

我想要完成的是过滤数组以仅包含包含 this.testID = "666666"; 的 UUID 我在 this.images = []; 开始处理我的过滤器 我不确定过滤器将如何应用于 this.images = []; 如有任何帮助,我们将不胜感激。

图片来自 xcode 日志

let filtered = arr.filter(x => x.name.includes('UUID_' + this.testID));

StackBlitz example

注意数组结构 - 添加的对象类似于

{name: 'auditid_626_UUID_666666_time__1582577405550.jpg'}

您的列表必须是这样的:

let list = [
 "auditid_626_UUID_666666_time__1582577405550.jpg",
 "auditid_626_UUID_999999_time__1582577405554.jpg",
 "auditid_626_UUID_999999_time__1582577405557.jpg"
];

然后试试这个:

loadStoredImages() {
  this.storage.get(this.STORAGE_KEY).then(images => {
    if (images) {
      let arr = JSON.parse(images);
      this.images = this.images.filter(a => a.includes("UUID_" + this.testID));
      for (let img of arr) {
        let filePath = this.file.dataDirectory + img;
        let resPath = this.pathForImage(filePath);
        this.images.push({ name: img, path: resPath, filePath: filePath });
      }
    }
  });
}

示例:

let list = ["auditid_626_UUID_666666_time__1582577405550.jpg",
 "auditid_626_UUID_999999_time__1582577405554.jpg",
 "auditid_626_UUID_999999_time__1582577405557.jpg"
];
const result = list.filter(a => a.includes("UUID_666666"));
console.log(result);

使用 Oussails 回答我使用了以下代码。请注意,我必须将过滤器移动到变量 arr 而不是 this.images

loadStoredImages() {
  this.storage.get(this.STORAGE_KEY).then(images => {
    if (images) {
      let arr = JSON.parse(images);
      this.images = [];
      arr  = arr.filter(a => a.includes("UUID_" + this.testID));
      for (let img of arr) {
        let filePath = this.file.dataDirectory + img;
        let resPath = this.pathForImage(filePath);
        this.images.push({ name: img, path: resPath, filePath: filePath });
      }
    }
  });
}