What is the way to retrieve an array property in a typescript interface?

Imagine a scenario with three interfaces structured as follows:

registration-pivot.ts

export interface RegistrationPivot {
    THead: RegistrationPivotRow;
    TBody: RegistrationPivotRow[];
}

registration-pivot-row.ts

export interface RegistrationPivotRow {
    CellList: RegistrationPivotCel[];
}

registration-pivot-cel.ts

export interface RegistrationPivotCel {
    content: string;
}

If I fetch data from an API and assign it to the registrationPivot, how can I access the values in my HTML?

registrationPivot: RegistrationPivot = {} as RegistrationPivot;

searchRegistrationStatistic(){
    this.registrationApi.getAllRegistrationStatistic().subscribe(res => {
    this.registrationPivot = res;
    });
}
  • I attempted to use registrationPivot.THead.CellList, but that did not work.
  • I need to iterate through all the Cells within the THead element (as well as TBody), but I am unsure of how to accomplish this. Can someone please provide guidance on how to achieve this? Thank you!

Answer №1

The way you have structured your interfaces and data access methods is accurate. An example demonstrating this can be seen below (executed in a browser on JSFiddle).

interface RegistrationPivotCel {
  content: string;
}

interface RegistrationPivotRow {
  CellList: RegistrationPivotCel[];
}

interface RegistrationPivot {
  THead: RegistrationPivotRow;
  TBody: RegistrationPivotRow[];
}

const example = {
    THead: {
    CellList: [
      {content: "title"}
    ]
  },
  TBody: [
    {
      CellList: [
        {content: "row A"},
        {content: "row B"}
      ]
    }
  ]
}

console.log(example.THead.CellList)
/*
[
  {
    "content": "title"
  }
]
*/

The data being returned from

this.registrationApi.getAllRegistrationStatistic()
may not adhere to these defined interfaces. You should try using console.log to inspect the res object and verify if it conforms to the expected structure.

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

CSS and Javascript functioning correctly within internal server, but encountering issues when accessed externally

I am in the process of creating a website for a friend. The goal is to have a flashy animated style website that functions well on IOS and allows him to make changes easily. To achieve this, I am utilizing JQuery, my own javascript, and multiple css files. ...

Preserving the HTML format of a string stored in MongoDB: Best practices

Currently, I am utilizing AngularJS for my frontend and mongodb for backend database management. For post editing, I rely on textAngular. When creating a new post, such as a service agreement, textAngular automatically adds formatting using HTML tags. &l ...

showing errors in case the username does not match the specified pattern

As I work with symfony, one of the challenges is displaying errors when the username does not fit the specified pattern. How can this be achieved? {{ form_widget(form.username, {'attr':{'pattern': '[a-zA-Z]*'} }) }} ...

Implementing a boolean value in PrimeNG's p-dropdown version 7.x

I need a p-dropdown with two options to be selected based on the boolean value of the control. The control (NOTIF_ALL) is assigned a boolean value oldPendingTasksOptions=[ {"oldPendingTaskId": false, "oldPendingTasksName": "Not a ...

Exploring variables within an Angular2 component's view

Is there a way for me to access and display the coordinates in my template? export class DashboardPage { constructor(public navCtrl: NavController) { Geolocation.getCurrentPosition().then(pos => { console.log(pos.coords.latitude, pos.coor ...

bespoke filter designed to conceal any negative figures

I am trying to implement a feature where a text box will display nothing if the ng-model contains a negative value. I want the ng-model to remain unchanged while ensuring that negative values are not displayed. I am looking for a custom filter to achieve t ...

Adding HTML elements to a button using an array: a step-by-step guide

In the process of developing a web application feature using JavaScript, I have come up with a simple plan: Place a button in the bottom left corner. The button should only become visible after scrolling begins. Upon clicking the button, a new window wil ...

How can I add a JavaScript-created element into a Primeng TurboTable component?

I am in the process of replacing a custom-made table with PrimeNG's turbotable. The issue I'm facing is that when I try to insert buttons into the table that call specific JavaScript functions, they end up displaying as [object HTMLInputElement] ...

How can I maintain focus selection while replacing HTML with text in a contenteditable div without losing it?

When working with a div tag that needs to be editable, the goal is to prevent users from entering any HTML into the tag. However, despite efforts to restrict input, when users copy and paste content, it often includes unwanted tags. To address this issue, ...

I'm curious about the meaning behind this code snippet: `$Odd = ($Odd == "even") ? "odd" : "even";`. Any ideas?

<?php $Odd = "even"; $query = $MySQLi->query("SELECT id, look, username, motto FROM users WHERE rank = '7'"); if($query->num_rows > 0): while($UserRow = $query->fetch_assoc()) { $Odd = ($Odd == "even") ? "odd" : "even"; ?&g ...

What is the process for extracting the paths of component files from an Angular ngModule file?

I've been on the lookout for automation options to streamline the process of refactoring an Angular application, as doing it manually can be quite tedious. We're working on reducing our app's shared module by extracting components/directive ...

Tips for avoiding rule `@angular-eslint/template/i18n` from checking `mat-icon` tags

A strategy I implement is using the rule @angular-eslint/template/i18n to analyze template elements containing text nodes without an i18n attribute. In Angular Material, the identification of icon keys is done through the inner text of mat-icon elements, ...

Remap Objects Function with Correct Return Data Type

After receiving data from another source via a post request in a large object, I need to extract specific fields and organize them into more precise objects with some fields remapped before inserting them into a database. Currently, I have a working solut ...

Setting a const value (true or false) based on the size of the window - a step-by-step guide

While studying, I encountered a challenge: In the code below, I need to dynamically set useState(false) based on the screen size. If the screen is larger than 947px, for instance, it should automatically be changed to true. The issue arises because sett ...

What is the solution for correcting the fixed footer in jQuery Mobile?

My experience with jQueryMobile has led me to encounter a couple of persistent bugs even after including data-role="footer" data-position="fixed" in the markup: The footer toggles on a null click event. The footer remains unfixed and ends up hiding some ...

Utilizing NodeJS application to connect to Sharepoint 2019 On-Premises Web Services

Recently, I/T in my organization set up a new Sharepoint 2019 OnPromise with a hybrid configuration that utilizes Azure AD for authentication. As the site collection admin for our Sharepoint website, the URL to access it is Upon accessing this URL, I am ...

JavaScript or Query: Transforming an Object from an Associative Array

Can someone help me out with converting an associative array into an object in JavaScript? I tried searching on Stackoverflow but couldn't find a working example. Here is the example structure: var combinedproducts = [["Testing-1","test-1"],["Testin ...

What steps should I take to make the code in jsfiddle functional on my Visual Studio Code platform?

const canvasEle = document.getElementById('drawing-container'); const canvasPad = document.getElementById('pad'); const toolbar = document.getElementById('toolbar'); const context = canvasEle.getContext('2d'); const ...

How about placing a particle on the top layer of a mesh at a random location?

I am struggling to place a particle on the top side of a custom mesh using Three.js. Most tutorials I've come across only demonstrate adding particles to cubes or spheres, leaving me with no solution for my unique shape. How can I successfully positio ...

Help kids find solutions

Here is the HTML code I am working with: <div class = "touch" onclick="do(this)"> <span>text01</span> <span>text02</span> <span>text03</span> <span>text04</span> <div class = "findMe">a ...