problems encountered while looping through objects using *ngFor in Angular 2

I have an array in my subscribe function called this.Term. When I console log this array, it displays the following:

[Discount Condition:Array(3), Stay Consition:Array(2)]
Discount Condition:Array(3)
0:object
   EventType:"success"
   Heading:"Discount Condition"
   Help:"Terms"
1:object
   EventType:"success"
   Heading:"Discount Condition"
   Help:"Terms"
2:object
   0:object
   EventType:"success"
   Heading:"Discount Condition"
   Help:"Terms"

Stay Condition:Array(2)
0:object
1:object

In my component HTML file, I am trying to display this array using Angular's *ngFor directive like so,

<div *ngFor="let item of Term">
     {{item.Heading}}
</div>

However, when I check the rendered elements in the browser inspect tool, it shows:

<!--template bindings={
  "ng-reflect-ng-for-of": ""
}-->

This is the function responsible for populating the array:

getTerms(){
    this.quoteService.getTerms()
    .subscribe(
      terms => {
        this.terms = terms.resultData.Terms;
        this.notes = terms.resultData.ImpNotes;
        this.exclusions = terms.resultData.Exclusions;
        var arr=[];
        var strHeading;
        for(let i=0;i<this.terms.length;i++) {
          strHeading=this.terms[i].Heading;
          if (arr.indexOf(strHeading) >= 0) {
            this.uniqTerm[strHeading].push(this.terms[i]);
          } 
          else {
            if (!this.uniqTerm[strHeading])
               this.uniqTerm[strHeading] = [];
               this.uniqTerm[strHeading].push(this.terms[i]);
               this.Term.push(this.uniqTerm);
               arr.push(strHeading);
          }         
        }
        console.log(this.Term);
      },
      response => {
        if (response.status == 404) {
          //this.router.navigate(['NotFound']);
        }
      });
  }

Can someone please help me identify where I might be making a mistake?

Answer №1

It seems that this.Term is structured as an array of arrays.</p>

<p>For example:</p>

<pre><code><div *ngFor="let item of Term[0]">
     {{item.Heading}}
</div>

You can do a quick check to verify if your data is correct using the code above.

If you need to iterate through these arrays, you can try:

<div *ngFor="let item of Term">
     <div *ngFor="let elem of item">
           {{elem?.Heading}}
     </div>
</div>

This approach might be helpful in iterating over nested arrays.

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

Link a YAML file with interfaces in JavaScript

I'm currently learning JavaScript and need to convert a YAML file to an Interface in JavaScript. Here is an example of the YAML file: - provider_name: SEA-AD consortiumn_name: SEA-AD defaults: thumbnail Donors: - id: "https://portal.brain ...

Determining User Existence in AWS DynamoDB with Node.js Before Creating New Record

Currently, I am in the process of developing an AWS Lambda function to create a new customer (lead). However, prior to the creation of the customer, there is a need to perform a check to determine if the user already exists. The identification of a custome ...

When utilized in a nested component, the service is found to be null

When developing a nested component, I encounter an issue where the injected service is null. How can I successfully use a service in a nested component? export class ActivityComponent implements OnInit { constructor( . . public accountServ ...

The dynamic duo of Typescript and Express creates an unbreakable bond within a configuration

Trying to incorporate ES6 modules into my app has been a bit frustrating. Initially, I attempted setting "module": "es2020" or "module": "esnext", only to encounter an error instructing me to specify "type": "module" in the package.json file or use the .m ...

Angular Material select all checkboxes provide a convenient way to select multiple

Need help with implementing a select all checkbox on Angular Material. The goal is to have the master checkbox show Indeterminate when a specific item is clicked, and then turn to checked once all checkboxes are selected. Currently experiencing some unexpe ...

Incorporate OpenLayers 4 into your Angular 5 application

I'd like to integrate OpenLayers 4 into Angular 5 for a project. My main goal is to implement the QuickStart example provided on the official OpenLayers Site. This is what I have accomplished so far: npm install ol --save to download the OpenLayer ...

Assuming control value accessor - redirecting attention

import { Component, Input, forwardRef, OnChanges } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; @Component({ selector: 'formatted-currency-input', templateUrl: '../v ...

Retrieve a specific attribute from a collection of JSON objects and transfer it to a separate object

Having a JSON object array with various project information: [ {"Project":"Project 1","Domain":"Domain1","Manager":"Manager1"}, {"Project":"Project 2","Domain":&q ...

Error: Unable to dispatch a Redux-thunk action with additional arguments in React

Currently, I am attempting to work with TypeScript alongside Redux-Thunk and Redux. My goal is to pass an object to any action (I am utilizing dependency injection and need to pass either an object service or a string). However, I encountered the followin ...

Tips for customizing Material UI CSS default properties in React

I'm currently working on a React project and utilizing the 'Table' component from Material UI. The default CSS properties of this table, along with its components like TableHead, TableCell, and TableRow, are proving difficult to override whi ...

Angular provides a convenient way to call an API on key press. Let's delve

How can I trigger an API call in Angular when a user clicks on a textbox during a keypress event? I am encountering an error with the debounce method that says Cannot read property 'valueChanges' of undefined app.component.ts ngOnInit() { t ...

Error: Attempting to access the 'presence' property of an undefined value

Having trouble with calling loading/Toast/Alert in Ionic2 under the current scenario. Being a newcomer to Ionic development, I'm struggling to understand it. I realize it's a trivial mistake. var dg = document.getElementById('btnregis&apos ...

Achieving the desired type of value within a nested record

I am trying to create a unique function that can manipulate values of a nested Record, based on the collection name. However, in my current implementation, I am facing difficulty in attaining the value type: type Player = { name:string } type Point = { x:n ...

How to add a service to a static function in Angular

After incorporating a logger service into my project, I have encountered an issue with using it in NGXS static selectors. The selectors in NGXS are static methods, which prevent me from accessing the logger service injected via Angular DI. Are there any e ...

Leveraging Sat-popover within *ngFor loop in Angular 6

I have been attempting to implement sat-popover within an *ngFor loop in order to display multiple instances of sat-popover for various buttons. Here is the link to sat-popover However, I am encountering issues with getting it to work properly. Assistanc ...

Loading external libraries in Angular2: A step-by-step guide

I'm currently working on incorporating a Datepicker in Angular 2, but I'm facing an issue where the library is not loading. This is causing a template parsing error stating 'material-datepicker' is not a recognized element: My System.c ...

"Encountering the error of 'require is not defined' in an Electron-React-Webpack-Typescript application when utilizing

When I add these lines to /src/renderer.ts in an Electron-React-Webpack-Typescript app: ipcRenderer.on('messageFromMain', (event, message) => { console.log(`This is the message from the second window sent via main: ${message}`); }); I encou ...

Retrieve all articles from a user using the TypeORM - findAll function

Is there a way to retrieve all articles of a specific user using the TypeORM package? In Sequelize, I have the following function: async findAllByUser(userUuid: string, findOptions: object): Promise<Article[]> { return await Article.findAll< ...

When you click on one checkbox, the angular2-multiselect dropdown automatically selects all the boxes

<angular2-multiselect [data]="sortedDataList | OrderBy : 'clientName'" [(ngModel)]="selectedItem[sortedDataList.clientId]" [settings]="dropdownSettings" name="multiSelect" (onSelect)="onItemSelect($event, ...

Autoformatting files with ESLint on save

I'm encountering an issue where Visual Studio Code is saving my file in violation of the rules specified in my eslint configuration when using eslint and prettier for formatting. module.exports = { env: { browser: true, es2022: true, nod ...