Using Typescript for-loop to extract information from a JSON array

I'm currently developing a project in Angular 8 that involves utilizing an API with a JSON Array. Here is a snippet of the data:

   "success":true,
   "data":{
      "summary":{
         "total":606,
         "confirmedCasesIndian":563,
         "confirmedCasesForeign":43,
         "discharged":43,
         "deaths":10,
         "confirmedButLocationUnidentified":0
      },
      "regional":[
         {
            "loc":"Andhra Pradesh",
            "confirmedCasesIndian":9,
            "confirmedCasesForeign":0,
            "discharged":1,
            "deaths":0
         },
         {
            "loc":"Bihar",
            "confirmedCasesIndian":4,
            "confirmedCasesForeign":0,
            "discharged":0,
            "deaths":1
         },
         {
            "loc":"Chhattisgarh",
            "confirmedCasesIndian":1,
            "confirmedCasesForeign":0,
            "discharged":0,
            "deaths":0
         },
         {
            "loc":"Delhi",
            "confirmedCasesIndian":30,
            "confirmedCasesForeign":1,
            "discharged":6,
            "deaths":1
         }
      ]
   },
   "lastRefreshed":"2020-03-26T04:18:02.528Z",
   "lastOriginUpdate":"2020-03-25T13:15:00.000Z"
}

component.ts



  constructor(private _IndiaApiService: IndiaApiService) { }

  info:IndiaClass[]
  regional:[];

  ngOnInit() {
    this._IndiaApiService.getIndiaData()
    .subscribe
    (
      data=>
      {
        this.info = data;
      }
    )
  }

 

component.html

<table border=1>
            <tr>
                <th>State</th>
                <th>Recovered cases</th>
            </tr>

            <div *ngFor="let item of info.data.regional">
              <tr>
                  <td>{{ item.loc }}</td>
                  <td>{{ item.discharged }}</td>
              </tr>
            </div>
</table>

The code above displays a table with data from the first element of the array, but now I need to iterate through all 25 elements using *ngFor directive. How can I achieve this efficiently? Any alternative methods are also welcome. Thank you.

Answer №1

I am having a bit of trouble grasping exactly what you are seeking. However, perhaps you could experiment with this suggestion

<div *ngFor="let item of data.items">
  <p>{{ item.name }}</p>
  <p>{{ item.quantity }}</p>
</div>

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

Creating a popup trigger in ReactJS to activate when the browser tab is closed

I am currently working on an enrollment form that requires customer information. If a user fills out half of the form and then attempts to close the tab, I want to trigger a popup giving them the option to save and exit or simply exit. While I have a jQue ...

Angular HTTP client fails to communicate with Spring controller

Encountered a peculiar issue in my Angular application where the HttpClient fails to communicate effectively with the Spring Controller. Despite configuring proper endpoints and methods in the Spring Controller, the Angular service using HttpClient doesn&a ...

promise not being returned by service method

In my Angular 8 application, I am facing an issue where the promise call does not return an exception when it occurs. Can someone help me understand how to make getRepApprovedName return a promise? When I use 'return http.get', I encounter a synt ...

Experiencing the error message "delete(...).then(...).error is not a function" while attempting to remove a file from Firebase storage using AngularFire and TypeScript

I'm trying to implement the code snippet from Firebase documentation to delete a file and then upload a new image in the same directory on Firebase Storage. However, I keep encountering an error message saying "delete(...).then(...).error is not a fun ...

Encountering a JavaScript/TypeScript issue that reads "Unable to access property 'Items' as it is undefined"

I encountered an issue with Javascript where I'm receiving an error message stating "Cannot read property 'Items' of undefined". The this keyword is consistently showing as undefined in the Base class. How can this problem be resolved? Coul ...

What is the best way to ensure that my variables are properly differentiated in order to prevent Angular --prod --aot from causing empty values at runtime?

While testing my code locally in production, the functions I've written are returning the expected values when two parameters are passed in. However, after running ng build --prod --aot, the variable name within the functions changes from name to t. ...

Subscribing to ngrx store triggers multiple emissions

Currently, I have an app with a ngrx store set up. I am experiencing an issue where, upon clicking a button, the function that fetches data from the store returns multiple copies of the data. Upon subsequent clicks, the number of returned copies grows expo ...

Why is Firebase Deploy only detecting 1-2 files? It might be due to the default index of Firebase hosting

I'm currently in the process of deploying my Angular App to Firebase Hosting. However, I am encountering an issue where it only displays the default firebase index hosting with no changes. To set up the deployment, I have used firebase init and speci ...

Consolidate various arrays of objects while eliminating duplicate items based on an optional property

Imagine having multiple arrays like these: const arr1 = [ { "id": "1", "type": "sales" }, { "id": "2", "type": "finance" } ] const arr2 = [ { "type": "s ...

Steps for adding an input box to an md-menu item

I'm looking for an option that allows me to either add an item to an existing list or create a new one, similar to YouTube's 'Add to playlist' feature. The current implementation sort of works, but the menu disappears as soon as the inp ...

What is the process for converting language json files into different languages?

I'm currently using ngx-translate to localize an Angular app. With over 20 languages that need translation, I am in search of a tool that can efficiently translate the language json files. While I did come across a helpful website, it proved to be ti ...

What is the recommended approach for testing a different branch of a return guard using jest?

My code testing tool, Jest, is indicating that I have only covered 50% of the branches in my return statement test. How can I go about testing the alternate branches? The code snippet below defines a function called getClient. It returns a collection h ...

Is there a way to modify the style when a different rarity is selected in Next.JS?

Is there a way to change the style depending on the rarity selected? I am currently developing a game that assigns a random rarity upon website loading, and I am looking to customize the color of each rarity. Here is how it appears at the moment: https:/ ...

What is the best way to exclude a particular subtype or property?

Is there a way to exclude a specific nested property? Let's take a look at an example. interface ILikes { article: string, page: string, userId: number | string, } interface IUserData { userName: string, isAdmin: boolean, ...data, ...

Can Angular's change detection be triggered by a change in @Input?

As of now, I am immersing myself in guides and tutorials on Angular's change detection. There are some statements that are quite perplexing to me. Therefore, I am seeking confirmation or clarification. The default Change Detection is activated "every ...

Encountering an error while compiling the Angular 8 app: "expected ':' but got error TS1005"

As I work on developing an Angular 8 application through a tutorial, I find myself facing a challenge in my header component. Specifically, I am looking to display the email address of the currently logged-in user within this component. The code snippet fr ...

When utilizing an object as state in React, the `setState` function will only set

Currently, I am working on developing a React form that utilizes a custom Input component which I have created multiple times. The objective is to gather all the names and values from the form in the parent component: {inputName: value, inputName2: valu ...

Customizing a generic method in typescript

I am currently working on developing a base class called AbstractQuery, which will serve as a parent class to other classes that inherit from it. The goal is to override certain methods within the child classes. class AbstractQuery { public before< ...

Is there a sweet TypeScript class constructor that can take in its own instance as an argument?

I have a scenario where I need to read in instances of Todo from a CSV file. The issue is that Papaparse does not handle dynamic conversion on dates, so I'm currently dropping the object into its own constructor to do the conversion: class Todo { ...

Sorting through items within a container object

I am currently working with an object named 'docs' that contains multiple objects within it. I am attempting to determine the count of entries that have specific values for both 'exopp_rooms_id_c' and 'is_active'. While this m ...