What is the best way to sort through an Array of photo filenames?

https://i.sstatic.net/04cws.pngI have a list of image names that contain UUIDs.

images

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

These images are being displayed on an HTML page using the following code:

<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>

The method used to fetch and filter this array is shown below:

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

loadStoredImages() {
  this.storage.get(this.STORAGE_KEY).then(images => {
    if (images) {
      let arr = JSON.parse(images);
      this.images = [];  
      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 });
      }
    }
  });
}

The goal is to filter the array to only include the UUIDs that match

this.testID = "666666";
. Assistance is needed on applying this filter. Thank you!

image from xcode log

Answer №1

const filteredArray = originalArray.filter(item => item.name.includes('UUID_' + this.testID));

Link to live example on StackBlitz

Note the structure of the array - objects are formatted like:

{name: 'auditid_626_UUID_666666_time__1582577405550.jpg'}

Answer №2

Make sure your list follows this format:

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

Next, give this a try:

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

Here's an example to illustrate:

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

Answer №3

After implementing Oussails' solution, I made some adjustments to the code. It was necessary to move the filter operation to a variable called arr instead of directly accessing 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 });
      }
    }
  });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to reset an Angular 7 reactive form to its initial values instead of clearing them completely

Is there a way to restore a reactive form group back to its original values without completely clearing it using .reset()? In this StackBlitz example, the default selected value is Parent. If a user changes it to "sister" and wants to revert the form to i ...

What are the steps to add code into the Monaco Editor using Playwright?

As I explore the world of Playwright, I am faced with a challenge regarding testing a feature that involves a monaco editor. Unfortunately, my search in Playwright documentation and forums did not yield any relevant information. Here is the test scenario ...

Using Angular-cli in conjunction with a different server

As a newcomer to Angular 2, I am in the process of creating an app with the angular-cli system. While I can successfully serve the application using ng-serve, I am finding it quite challenging to serve it with anything other than this system. My current st ...

Master Angular Autocompletion

Looking to add a filter autocomplete feature but unsure of how to implement it. Any assistance would be greatly appreciated! I've come across some examples, but they don't quite fit my needs. Here's the snippet of code: <mat-form-field c ...

Steps to link two mat-autocomplete components based on the input change of one another

After reviewing the content on the official document at https://material.angular.io/components/autocomplete/examples I have implemented Autocomplete in my code based on the example provided. However, I need additional functionality beyond simple integrati ...

When the mat-tree collides with a stylish css grid

Is there a way to transform a lengthy checkbox tree into a more manageable grid layout? The main issue is the lack of grouping wrappers, with only partial padding indicating group relationships. Check out this StackBlitz link for reference It seems that ...

Button for saving content located in expo-router header

I am a beginner in react native and I am attempting to connect a save button in a modal header using expo-router. I have managed to make the button work within the modal itself, but I would like it to be located in the header set by expo-router. It doesn&a ...

The code for filtering an array in Angular 9 seems to be failing to display the expected filtered items

Here is the JSON data: [ { "products": [ { "id": "1", "name": "Apple", "price": "free", "category": "Fruits" }, { And so on And this : [{ "categories": [ ...

Taunting a specific occurrence inside a group

Good evening, I am currently in the process of developing tests for the TypeScript class shown below. My goal is to create a test that ensures the postMessage method of the internal BroadcastChannel is called. However, I am facing difficulties in setting ...

Leveraging jQuery within Angular 6

In the 'src' folder of my Angular app, there is a simple JavaScript file named 'my.js' slideDown(id) { $('#' + id).slideToggle(); } I am trying to figure out how to call this function by using the following code: <div ...

Issue with Angular routing: Content from app.component is displaying in all components

Can anyone help me with the issue I'm having regarding showing app.component content on every component? Here is a snippet of my app.component code: I want to exclude the navbar and app-users from being displayed in other components, but unfortunatel ...

Tips for extracting individual fields from a payload in Angular 8

How do I extract individual fields from the payload and bind them with HTML on a page? I am trying to retrieve the alphacode and countryname in Angular 8. This is my Component: this.table$ = this.productService.get(id) .map(actions => { c ...

The evaluation of mongodb-memory-server is experiencing issues with either failing or timing out

While setting up mongodb-memory-server in my backend for testing purposes, I encountered some issues during test execution that require debugging. The problem arises when running a test that creates a MongoDB document within the service being tested, leadi ...

What could be causing the div to be wider than the content inside of it?

I am having an issue creating a webpage with a 20-60-20 flex fill layout. The div in the center, which should occupy 60% of the page, is wider than its content, causing it to appear off-center. https://i.stack.imgur.com/WwCJy.png Here is my home.componen ...

Guide on setting an instance property through a callback function triggered by the instance

I am looking to set an attribute of an angular service using the result from a callback function. Here is my Angular service: @Injectable() export class SkillsManagerService { private skill: any; private event: any; constructor() { ...

What is the solution to fixing the error message "Cannot redeclare block-scoped variable 'ngDevMode' "?

I encountered this error: ERROR in node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'. src/node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451 ...

Unfortunately, the utilization of an import statement outside a module is restricted when working with Electron

Is there a solution to the well-known problem of encountering the error message "Cannot use import statement outside a module" when working with an Electron-React-Typescript application? //const { app, BrowserWindow } = require('electron'); impor ...

Adjusting the array when items in the multi-select dropdown are changed (selected or unselected)

I am looking to create a multi-select dropdown in Angular where the selected values are displayed as chip tags. Users should be able to unselect a value by clicking on the 'X' sign next to the chip tag, removing it from the selection. <searcha ...

Node.js, sigma.js, and the typescript environment do not have a defined window

When attempting to set up a sigma.js project with node.js in TypeScript, I encountered a reference error after starting the node.js server: ts-node index.ts The problem seems to be located within the sigma\utils\index.js file. <nodejsproject& ...

The type 'Text' does not have a property named 'then'

Transitioning from .js to typescript. When I changed the file extension from .js to .ts while keeping the same code, I encountered an error stating Property 'then' does not exist on type 'Text'.ts in the then((value) method. The return ...