我如何过滤照片名称数组?

问题描述 投票:0回答:2

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

图像

[
 { name: "auditid_626_UUID_666666_time__1582577405550.jpg" },
 { name: "auditid_626_UUID_999999_time__1582577405554.jpg" },
 { name: "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 = [];在这方面的任何帮助将不胜感激。

angular typescript ionic4
2个回答
2
投票
let filtered = arr.filter(x => x.name.includes('UUID_' + this.testID));
StackBlitz example

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

{name: 'auditid_626_UUID_666666_time__1582577405550.jpg'}


1
投票
您的列表必须是这样:

let list = [ {name: "auditid_626_UUID_666666_time__1582577405550.jpg" }, {name: "auditid_626_UUID_999999_time__1582577405554.jpg" }, {name: "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.name.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 = [ {name: "auditid_626_UUID_666666_time__1582577405550.jpg" }, {name: "auditid_626_UUID_999999_time__1582577405554.jpg" }, {name: "auditid_626_UUID_999999_time__1582577405557.jpg"} ]; const result = list.filter(a => a.name.includes("UUID_666666")); console.log(result);
© www.soinside.com 2019 - 2024. All rights reserved.