Angular - struggling to properly sort incoming data based on property with getter and setter functions

Within my application, there exists an array of objects containing a boolean property that is subject to change. Whenever this property changes, I use the .next(changedData) method to emit the updated array to the component that is subscribed.

The component responsible for monitoring these data changes must filter the data appropriately in order to prepare it for use with *ngFor.

I have been attempting to grasp the concept of using setter/getter methods effectively, as I wish to avoid using pipes due to potential inefficiencies when dealing with large amounts of data.

Although I came across this informative answer, I am still struggling to fully understand the proper implementation.

    export class MyComponent implements OnInit {
    
      private _listFilter: Data[];
        get listFilter(): Data[] {
          return this._listFilter;
        }
        set listFilter(filter: Data[]) {
          this._listFilter = filter;
          this.filteredData = this.performFilter(this.listFilter); //*ngFor is set on filteredData 
        }
       .
       .
       .
      ngOnInit(): void {
        this.dataSub = this.dataService.DataList$.subscribe(result => {
          this.data = result;
          this.filteredData = this.data; //*ngFor is set on filteredData 
        });
      }
       .
       .
       .
      private performFilter(dataArr): Data[] {
      //isNewData  is the propery that has changed and I want to iterate only over a list of data which the boolean isNewData equals to true
        return dataArr.filter((data: Data) => data.isNewData === true ); 
      }

Answer №1

If you're looking to improve it, consider updating it to

return dataArr.filter((element: Data) => element.isNewData);

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

Implementing Typescript with React: Assigning event.target.name to state

I am facing an issue with a React state that has specific named keys defined in an interface... Despite trying a potential solution based on the state keys, I am still encountering an error... { [x: string]: string; }' provides no match for the sign ...

The Angular 7 template falls short of occupying the entire page

I am currently working on converting an HTML page into my Angular project. While it functions correctly in the HTML version, it does not fill up the entire page when transferred to Angular. You can view an example of this issue on StackBlitz at the followi ...

Upcoming 13.4 Error: NEXT_REDIRECT detected in API routes

Here is the code snippet from my /app/api/auth/route.ts file: import { redirect } from 'next/navigation'; export async function GET(req: Request) { try { redirect('/dashboard'); } catch (error) { console.log(error); ...

The checkbox in ng-2-smart-table will either be checked or unchecked based on the value of the column

I am working with an ng2-smart-table and I need the second column to be a checkbox that is initially checked or empty based on the binary value in the fourth column of my data object. Currently, my code looks like this: export class UsermanagementComponen ...

Can one mimic a TypeScript decorator?

Assuming I have a method decorator like this: class Service { @LogCall() doSomething() { return 1 + 1; } } Is there a way to simulate mocking the @LogCall decorator in unit tests, either by preventing it from being applied or by a ...

Issue: Failed to access the 'setDir' property of an undefined object

Greetings, I am a newcomer to Ionic/Angular and have been facing a particular issue for over 20 days now. I have created a small app and would like to implement multi-language support with both RTL and LTR directions. I followed the documentation provided ...

The connection between Parent and Child components within the Angular framework

Can changes made in a Child component automatically reflect in the Parent component when passing variables from parent to child? If we send any variable from parent to child and then make changes in the Child component, will these changes be automatica ...

Utilizing dual functions within the onChange event handler in React

I have a situation where I need to pass a function from a parent component to a child component through the onChange event, as well as another function in the child component to update its own state. How can I achieve this? Parent export function Fruits() ...

Changes made to one order's information can impact the information of another order

Currently, I am in the process of developing a unique shopping cart feature where users input a number and a corresponding product is added to a display list. Users have the ability to adjust both the price and quantity of the products, with the total pric ...

Angular version 16+ is incompatible with Three.js and does not function properly together

I am attempting to integrate three.js into my Angular 16 project and encountering the following error message: npm i @angular-three/core npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: [email&# ...

How to handle an already initialised array in Angular?

I came across an interesting demo on exporting data to Excel using Angular 12. The demo can be found at the following link: This particular example utilizes an array within the component TypeScript file. import { Component } from '@angular/core' ...

Reactive forms in Angular do not refresh or update automatically when a new error is added

Upon initializing my FormGroup in the ngOnInit() method, I invoke a validator function to ensure that the password and confirmPassword fields match. This is how it looks in TypeScript: regForm: FormGroup; constructor() { } ngOnInit() { this.regFo ...

Verification of German Numbers

Is there a way to validate a textbox for input in German number format? I searched through the Validators documentation, but I couldn't find any specific validators for numbers. Examples of German number formats: 0 12,5 100.000 100.000,25 ...

Retrieving the inner text of a dragged element using Angular Material's DragAndDrop feature

Can the inner text of a dragged element be retrieved and utilized in the "onDrop" function within Angular's cdkDragAndDrop feature? onDrop(event: CdkDragDrop<string[]>) { if (event.previousContainer === event.container) { moveItemIn ...

Issue arises with library dependencies: various libraries are reliant on distinct versions of a shared library

I have multiple libraries that are dependent on the webpack library. Currently, I am using version 4.79.1, but when I run `npm install` I receive the following warning: [email protected] requires a peer of webpack@^2.0.0 || ^3.0.0 but none is in ...

Issue TS2322: The type 'Observable<any>' cannot be matched with type 'NgIterable<any> | null | undefined'

Encountering an error while attempting to fetch data from the API. See the error image here. export class UserService { baseurl: string = "https://jsonplaceholder.typicode.com/"; constructor(private http: HttpClient) { } listUsers(){ //this ...

Break down a data structure into a type that includes multiple options

Is it possible for Typescript to support type or interface "destructuring" (if that's the right term)? I am attempting to achieve something similar to the code snippet below: type SomeRandomType = { propertyOne: string; propertyTwo: number; ...

Nested component in reactive forms not functioning as expected

I've been experimenting with creating nested reactive form components. Specifically, I'm working on a reusable input component for my app within a reactive form. How can I dynamically communicate with nested components in Reactive forms? Despite ...

Troubleshooting Angular4 and TypeScript Compile Error TS2453: A Comprehensive

I'm facing an issue in my Angular4 project build process which I find quite perplexing. The error seems to be related to the import of certain components such as Response, Headers, and Http from @angular. Whenever I attempt to build my project, it thr ...

Troubleshooting Async Function compatibility between Express and NestJs

Initially, I set up a small express server to handle report generation and file writing tasks. var ssrs = require('mssql-ssrs'); var fs = require('fs'); const express = require('express') const app = express() const port = 30 ...