Interacting with TypeScript properties

In my Angular 2 project, I have a specific object definition that includes properties for officeId, year, pageNumber, and pageSize.

export class MyFilter {

  public officeId: string;

  public year: number;

  pageNumber: number;

  pageSize: number;

  public constructor() {
    this.pageSize=10;
    this.pageNumber=10;
    this.year=2014;
    this.officeId='abc';
  }

}

I am trying to loop through the properties of this object using a for cycle:

let bean=new MyFilter();

for (const p in bean) {
  if (bean.hasOwnProperty(p)) {
      console.log(p + ': ' + bean[p]);
  }
}

I simplified the code here to keep it focused on the issue. How can I effectively iterate over its properties in TypeScript? The current implementation does not output anything.

Answer №1

Give this a shot

let x=new CustomHandler();
Object.keys(CustomHandler).forEach((operation) => {
console.log(x[operation]);
});

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

Unable to change the main data of slot object in VueJS

When running this demo and selecting "modify in child", the text will be updated. However, if you choose "modify top level through slot", the text remains unchanged, and attempting to click the other button afterwards will not work. Is there a way to upda ...

When using express, encountering a "Cannot GET / on page refresh" error

Currently working on a small MERN stack project. Managed to deploy it on Vercel successfully and the application runs as expected. Navigating to "/classes" and "/students" using the buttons in the browser works fine, however, upon reloading those pages I e ...

Exploring Angular 2's @Input and @Output Directives

I'm unsure about whether I should be using @Input and @Output. From my understanding, these decorators are typically used when you want to establish communication between a parent and child component. Can you confirm or correct me on this? I have 3 c ...

Can metadata be attached to data models in Angular for annotation purposes?

Looking to add some metadata annotations to a simple data model export class Certification { title: string; certificationType?: CertificationType; validTo?: number; description?: string; externalIdentifier: Guid; constructor() { ...

Achieve multiple returns of a function by employing .fadeOut() iteratively instead of just once

I'm not sure if it's supposed to behave this way, but I believe it is... Initially, I thought there might be an issue with my entire script so I created a new file on localhost to test just the fadeOut(); function. To my surprise, the function ...

Using jQuery to populate a select box with values from an XML file

I've come across various examples demonstrating how to achieve this task and have successfully applied others that utilize attributes. However, in the current scenario, I am working with XML data that does not contain attributes. My goal is to populat ...

What are the reasons for validation failure when it is moved into a method?

I am currently in the process of developing a validation function in JavaScript. However, when I extracted the logic into a private method, my validation started failing and I can't seem to figure out why. Here is my HTML definition: <input type= ...

Enhancing Nativescript with the latest JavaScript updates

The NativeScript manual states that TypeScript (.ts) files are automatically compiled to JavaScript during project build. However, in my Angular + TypeScript mobile application, I have observed that the compilation of .ts files to .js files does not alw ...

Creating image paths for a list of items in reactjs can be achieved by mapping through the list

My goal is to display a list of items, with each item being assigned an image based on its ID: private static renderItemsTable(products: Product[]) { return <table className='table'> <thead> <tr> ...

Generating Smoke Effects with Three.js

I am looking to generate visually appealing brown smoke rising into the sky. Any tips on how I can achieve this effect? ...

Issue: Invalid parameter: 'undefined is not a numeric value' for DecimalPipe

Here is the HTML input code that I am using: <input class="number " type= "text" pInputText [readonly]="" formControlName="id" [required]="" plmNumberFormatter [value]="data?.id | numberPipe" /> However, when I place the cursor on the input fiel ...

Issue with THREE.js: Object picking does not display the parent object name when an object is loaded using OBJMTLLoader

I successfully loaded an OBJ file along with MTL file textures using OBJMTLLoader. The code I used was borrowed from . The primary object, a man in a business suit with hair, hands, and shoes, is displayed correctly with all the textures applied, such as ...

Saving the selections from two drop-down menus into a single variable

I have a requirement to store the user selected values from two dropdown menus. These dropdowns are created using the selectize library in HTML: <div style="max-width: 200px"> <select id="dropdown-1" ...

Why does my JavaScript only trigger my web service request when I set a breakpoint?

Can you help me understand why my JavaScript code only calls my webservice when I set a breakpoint on the line ].getJSON, but not if I remove the breakpoint? $(function () { $("#" + @Model.BidObjectId).submit(function () { ale ...

Having trouble sending an array's JSON data to a web service using Angular

I am working with a table where each cell in the rows contains form fields. The table also has two buttons: one button adds a new row to the table, and the other sends all the rows. Below is the code snippet for adding new blank rows: $scope.attributes = ...

Strategies for improving the efficiency of this loop function?

var tickers = []; for (var i=0; i<reportsArray.length; i++) { tickers.push(reportsArray[i].ticker); } Is there a more efficient way to achieve the same result using lodash library? You can find more information about lodash here. Here is an examp ...

Receive JSON data with camel-case in a Web API 2.0 using a model in pascal-case style

My attempt to execute a PUT call on my Web API involves configuring the WebApiConfig.cs file to send data back to my Web project in camel case format. config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesCont ...

Why does the HTML and CSS slider fail to load upon page refresh, yet works perfectly when the next or previous button is clicked?

There seems to be an issue with the slider images not loading on page refresh. Oddly enough, they appear only after clicking the next button. However, upon refreshing the page again, the images disappear. <div class="slide-banner"> < ...

What are the benefits of using AngularJS 2 in a PHP environment?

1) Is it possible to develop a project using php and angularjs 2 together? 2) Can I configure the development environment for Angularjs 2 with php? ...

Using ngIf with Promises causes a malfunction

I have extensively tested this issue and I am unable to understand why the code below is not functioning as expected. The problem arises when the @Input variable is received and the user object is fetched from the service, the ngIf directive in the templat ...