Creating a new array for an Angular 5 class instance

My goal is to create and initialize an array of a class in Angular 5.

export class B{

  public Colors: string;
  public Temp: number;
  public Numbers: number;

}

export class A{

  public MyTable: B[];
  public othervar: number;
  ...
  ...
}

Next, I proceed to initialize MyTable using the following code:

var test = new A();

test.MyTable[0].Numbers = 25;
test.MyTable[0].Colors= "blue";

Is this the correct way to do it?

Answer №1

Another way to utilize the new B() function:

const example = new X();

example.Table = [];
example.Table[0] = new B();
example.Table[0].Values = 50;
example.Table[0].Shades= "red";

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

Receiving warnings during npm installation and encountering difficulties with fixing issues using npm audit fix

Currently, I am working on developing an Angular application with a .NET Core Web API integration. Upon cloning the repository, I attempted to execute 'npm install' for the Angular application, but encountered an unexpected error: npm install n ...

Converting JSON Arrays into Typed Arrays in NativeScript using Typescript

Can someone assist me with a problem I'm facing while learning NativeScript? I am trying to read a txt file that contains JSON data and then assign it to an Array called countries. However, despite my efforts, I have not been successful. The code sni ...

Where Should akitaConfig Be Placed in Angular Development?

Despite my attempt to place akitaConfig directly in the constructor of my app.component.ts file, I am encountering issues with it not properly configuring the data stores created afterwards. My goal is to set resettable to be universally true. Currently, t ...

In the Safari browser, an error occurred when trying to locate the variable Tmpo, which is a plain JavaScript class

I created a small Angular application that utilizes a basic JavaScript class located in my assets/js directory. Everything functions flawlessly in my local environment when using ng-serve. However, upon building and deploying the app (ng build --prod), I e ...

Limiting JSDoc/TypeScript type to a specific array element

Utilizing TypeScript with JSDoc poses a challenge as I aim to restrict a variable to one of the known values stored in an array. I am aware that it can be achieved like so: /** @type {'one'|'two'|'three'} */ let v = 'fo ...

This error message indicates that the certificate for Angular 13 has expired

After successfully installing Angular and verifying the version, I encountered an issue when attempting to run the command npm start. Despite multiple attempts at uninstalling and reinstalling everything, the problem persists. Running npm install also resu ...

Provide a definition for constraining type parameters in the inclusion of static methods

Is it possible to set type parameter restrictions for a serializer where the type must have a specific static member or implement an interface "statically"? For example: function getStuff<T>(...) -> T { T.buildFormJson(getStuffJson(...)) } He ...

Tips on creating a hierarchical ul list from a one-dimensional array of objects

I have an array filled with various objects: const data = [ {id: "0"},{id: "1"},{id: "2"},{id: "00"},{id: "01"},{id: "02"},{id: "11"},{id: "20"},{id: "23"},{id: & ...

Issue encountered in Angular 2 Heroes Tour, Step 7

I checked multiple forums, but didn't come across any relevant information. After starting the tutorial just 3 days ago and reaching Step 7, I encountered an error when incorporating the InMemoryWebApiModule as instructed in the steps. The error can ...

To utilize the serve command, you must be within the constraints of an Angular CLI project. Encounter an error when trying to run a dynamic web project

Trying to develop a dynamic web project using Angular 2, I started by creating a dynamic web project in Eclipse EE Neon. I then right-clicked on the project and selected 'Show In' -> 'Terminal'. However, when I ran the ng serve comma ...

Issue with assigning objects to an array

I'm currently working on a TypeScript application and I've run into an issue with assigning values. Here are the interfaces for reference: export interface SearchTexts { SearchText: SearchText[]; } export interface SearchText { Value: st ...

Increase the ngClass attribute's value

Is there a way to automatically increment a numeric value in a class using the ngClass directive? For example, can we achieve something like this: <some-element [ngClass]="'class-*'">...</some-element>, where the asterisk (*) will in ...

What is the best way to troubleshoot a quasar typescript file type error?

Currently, I am delving into Quasar using TypeScript and encountering a type error while working on file uploads. Here is the snippet of my code where the type error arises specifically in the parameter of the form.append() method. The error message read ...

Error encountered during installation of Nativescript Post Install Script

While I am comfortable running one of our current projects with Nativescript, I encountered an error when attempting to install it on a new project using the following command: sudo ng new --collection=@nativescript/schematics the-juice-box --shared The ...

Is it possible to eliminate Image Exif data and correct the orientation using Angular or JavaScript?

How can I ensure that when a user selects an image, the EXIF orientation is fixed and then removed so it uploads to my file server in the correct orientation without any meta data attached? I have only come across solutions for preview files so far. Can a ...

Generate a two-digit number within a looping structure

Hey there, I have a task where I need to generate numbers within a for loop. For numbers 1 to 9, I want to prepend '0' to them so they appear as 01, 02, 03...09, 10.... Here is how I approached it: for (var a = 1; a < 30; a++) { ...

This object does not have support for the attribute or method "getAttribute"

I've searched for solutions, but nothing seems to work for me and now I'm feeling quite lost. My current setup involves Cordova, Ionic, and Angular 2. Below is the HTML snippet: <ion-col *ngFor="let value of myButtonsFirstRow" width-25> ...

Using Angular 2, you can pass an object as a parameter to a function

Is there a way to pass an object as a parameter in the DOM on this forum? Within my HTML code, I have the following: <div class="list-items"> <ul> <li *ngFor="let i of item"> <span (click)="onAdd({{newUser.us ...

What causes the frustratingly slow transition between two components on Android?

I am currently in the process of developing a compact puzzle app that resembles a crossword using Expo, React Native, and Typescript. Below is a concise version of the PuzzleMain component: const PuzzleMain: React.FC<PuzzleMainProps> = ({ navigation ...

Issue with TypeScript: constraint mismatch in generic index lookups

In this particular scenario, I cannot envision a situation in which this approach would not be appropriate. Consequently, my expectation is that it should be permissible. Could someone provide clarification on why this approach is restricted? ...