Exploring Angular RxJS: the art of filtering Observable arrays

Is there a way to effectively filter an array Observable in Angular? I am working with an observable Array:

announcementList$: Observable<Announcement[]> = this.announcementService.getAnnouncement(0,0).pipe(filter(Boolean),shareReplay(), map(({data}) => data));

Upon receiving a response from the server, the array looks like this:
        {
            "id": 109,
            "title": "Title 231231242342342342342342342",
            "body": "testing",
            "id_creator": 1,
            "creator_login": "landrynek",
            "created_at": "2020-04-08 13:58:45",
            "updated_at": "2020-04-08 14:48:24",
            "status": 1,
            "accepted_at": null,
            "deleted_at": null
        },
        {
            "id": 110,
            "title": "Title 231231242342342342342342342",
            "body": "testing",
            "id_creator": 1,
            "creator_login": "landrynek",
            "created_at": "2020-04-08 14:24:25",
            "updated_at": "2020-04-08 14:41:09",
            "status": 1,
            "accepted_at": null,
            "deleted_at": null
        },
        {
            "id": 112,
            "title": "Title 231231242342342342342342342dsaasassdadasda",
            "body": "testing sadasasa",
            "id_creator": 1,
            "creator_login": "landrynek",
            "created_at": "2020-04-08 14:24:34",
            "updated_at": "2020-04-08 14:48:55",
            "status": 1,
            "accepted_at": null,
            "deleted_at": null
        }

The filter array:

 filteredData$:Observable<any[]>;

My interface Announcement:

export interface Announcement{
    id: number;
    title: string;
    body: string;
    id_creator: number;
    creator_login: string;
    created_at: Date;
    updated_at: Date;
    status: number;
    acepted_at: Date;
    deleted_at:Date;
    price?: number;
    number_of_workers?: number;
    available?: Date;
    end_available?: Date;
}

Incorporating into HTML and my component.ts file:

<input type="text" (keyup)="updateFilter()" [formControl]="inputControl" required />

(component.ts)
inputControl = new FormControl();

Method used for filtering:

updateFilter() {
    this.filteredData$ = combineLatest(
      this.announcementList$,
      this.inputControl.valueChanges.pipe(
        startWith(""),
        debounceTime(400)
      )
    ).pipe(
      distinctUntilChanged(),
      filter(([list, input]) => Boolean(list)),
      map(([list, input]) => {
        return list.filter(
          ({ creator_login }) => creator_login === input);
      })
    );
    this.announcementList$ = this.filteredData$;

    this.table.offset = 0;
  }

When typing any string in the input field, the data is not being filtered.. How can I properly filter and display results in my HTML table? When I console.log the 'list' in the last filter, I see all data in the array but the input remains empty..

Answer №1

Implement the FormGroup, FormBuilder, and FormControl structure within a ReactiveForm for effective form handling.

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

I'm encountering a Typescript error where I'm unable to assign a function to RefObject.current and it's indicating that the function is not callable

Does anyone know why assigning a function type to a ref.current type is causing me issues? useEffect(() => { savedHandler.current = handler; // ERROR HERE: }, [handler]); TS2741: Property 'current' is missing in type '(e: Chang ...

What is the best way to keep track of a checkbox's value after unchecking it and then returning to the same slide?

Issue: By default, the checkbox is always set to true in the backend code. Even if I uncheck it using JavaScript, the value remains as true when switching between slides. Desired Outcome: If I uncheck the checkbox, the updated value should be saved so tha ...

Angular - send multiple HTTP requests for the length of an array and combine the responses into one array

Exploring angular for the first time and dabbling with the trello API. I have an array containing some list IDs that I need to make HTTP GET calls for, equal to the length of the array. For instance, if there are two IDs in the array, then the HTTP call sh ...

What is the best way to create a TypeScript function similar to a 'map' in React?

I recently started learning TS and am having trouble typing this arrow function: const mapLikeGet = (obj, key) => { if (Object.prototype.hasOwnProperty.call(obj, key)) return obj[key] } ...

Set the mat-option as active by marking it with a check symbol

Currently, I am utilizing mat-autocomplete. Whenever a selection is made manually from the dropdown options, the chosen item is displayed with a distinct background color and has a checkmark on the right side. However, when an option in the dropdown is se ...

Errors in TypeScript are being brought up by using if-else statements inside a loop

I am currently working on a function to retrieve referral codes from users. The user inputs a code, which is then checked against the database to see if it exists or not If the code provided matches the current user's code, it should not be accept ...

Issue encountered with ngFor: 'Identifier not defined __type does not have any such member'

Recently, I successfully implemented a sub-component that allows users to dynamically add and remove a set of controls to and from a collection. The inspiration for this solution came from reading through this particular thread on Stack Overflow. While ev ...

Error Encountered during Compilation of React TypesIs this okay

Currently, I am working on an MVC project that involves the use of TypeScript. To access the types required for this project, I have also integrated React. To obtain the React types, I performed an npm install --save-dev @types/react (similarly for react-d ...

Integrate a service component into another service component by utilizing module exports

After diving into the nestjs docs and exploring hierarchical injection, I found myself struggling to properly implement it within my project. Currently, I have two crucial modules at play. AuthModule is responsible for importing the UserModule, which conta ...

Leverage TypeScript to generate a unified object that combines keys from multiple objects

Consider the object below: const myObject = { one: { fixed: { a: 1 } }, two: { fixed: { b: true } }, three: { fixed: { c: 'foo' } } } Is there a way to use this object to define a type simila ...

Proper positioning of popover ensures it does not exceed the boundaries of its parent

In my ngprime table, the header row contains a column field with a popover set to display at the top. However, it is covering the actual field instead of appearing above it. This issue arises because the popover cannot display outside of its parent div, ca ...

Register the canDeactivate guard dynamically and trigger a confirmation prompt for navigation

Is it possible for a component to autonomously set up the canDeactivate guard in order to prompt or prevent user navigation with confirmation options like Yes/No? I am exploring this possibility due to the large scale of the application and how workflow c ...

Challenge with React CloneElement regarding typing

Utilizing React Children and React Clone element, I aim to trigger methods in both the wrapper and Select components upon onClick event in the Option component. Although everything is functioning correctly, I am encountering a type error when calling the O ...

What is the process for creating a unique angular module using npm?

I've successfully created and published an npm module. Now, I'm facing issues in integrating it into my app using webpack to compile the angular universal setup. For Angular universal repository: https://github.com/angular/universal-starter/tre ...

Issues with rapid refreshing are arising in Vite while dynamically importing components with React and typescript

In my quest to develop a multistep form, I have organized my data in a separate file for constants as shown below: import { lazy } from 'react'; export const steps = [ { id: 0, name: 'Personal Info', component: lazy(() ...

The variable X has been defined, but it's never actually utilized. Despite declaring it, I have not accessed its

I have encountered warnings in VSCode while using certain properties in my Angular component. The warnings state: '_id' is declared but its value is never read.ts(6133) (property) ItemEditComponent._id: number | undefined '_isModeEdit' ...

Node.js: Managing multiple occurrences of the same event name for testing purposes

When it comes to unit testing using mocha, I am looking for a way to set up an asynchronous queue for handling events. Previously, I used a once() Promise to wait for events like this: import EventEmitter from 'events' import { once } from ' ...

Filter that caters to specific number of properties of X

Looking to create a versatile filter function that can handle multiple criteria? Here's a snippet of the current filtering function: const filterRows = () => { items.filter((item) => { if(selectedDrinks.length > 0 && select ...

Transform the MUI Typescript Autocomplete component to output singular values of a specific property rather than a complete object

When utilizing MUI Autocomplete, the generic value specified in onChange / value is determined by the interface of the object set in the options property. For instance, consider an autocomplete with the following setup: <Autocomplete options={top ...

A new feature introduced in TypeScript, expression-level syntax was not present until it was added in JavaScript

Celebrating a Decade of TypeScript remarked that "It’s quite remarkable how the design goals set for TypeScript have stood the test of time." I am particularly intrigued by the goal of "Avoid adding expression-level syntax." One user even brought up thi ...