I'm having trouble dynamically rendering table data with jSON information

I am working with the code shown in my component.ts file.

{
  Sheet1:
    [
      0:{Name: "foo", Age: "24", __rowNum__: 1},
      1:{Name: "boo", Age: "14", __rowNum__: 2}
    ]
}

The data from above is stored in the this.tableData array.

To dynamically display table headers, I store all keys in an array.

this.tableHeaders= Object.keys(this.data['Sheet1'][0]);

The resulting tableHeaders array will have values like:

["Name", "Age"]

I then place all necessary data for the table in another array.

this.data = this.tableData['Sheet1'];

Now, I aim to present this information in a table structure using the following setup:

<table>
   <tr>
      <th *ngFor="let header of tableHeaders">
        {{header}}                         /* able to show headers */
      </th>
   </tr>
   <tr *ngFor="let header of importedData">
                <td *ngFor="let item of data">{{header[item.value]}}</td>
            </tr> //here I want to display table data based on header value
 </table

If someone could provide assistance, it would be greatly appreciated.

Answer №1

Give this a try:

<table>
    <tr>
       <th *ngFor="let header of tableHeaders">
           | {{header}}
       </th>
    </tr>
        <tr *ngFor="let row of data">
            <td *ngFor="let key of tableHeaders">{{row[key]}}</td>
        </tr>
</table>

This code snippet should work for your needs!

UPDATE: Please note that the structure of your code block is incorrect.

{
    Sheet1:
        [
            0:{Name: "foo", Age: "24", __rowNum__: 1},
            1:{Name: "boo", Age: "14", __rowNum__: 2}
    ]
}

You need to remove the numerical indexes before the nested objects in the Sheet1 array first. Here's how it should look:

{
    Sheet1:
        [
            {Name: "foo", Age: "24", __rowNum__: 1},
            {Name: "boo", Age: "14", __rowNum__: 2}
    ]
}

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 shared module for Ionic 3 to handle pipes specifically for lazy loaded pages

Lazy loading in Ionic 3 is achieved using IonicPage and IonicPageModule, but unfortunately, lazy loaded pages do not have access to pipes. Failed to navigate: Template parse errors: The pipe 'myPipe' could not be found ("") This question a ...

Solving the Mailman Mailbox Dilemma with the Power of 2D Arrays and

In this particular software, the primary goal is to generate a boolean array consisting of 150 designated "mailboxes". The initial for loop aims to assign all mailboxes as "closed," represented by false. Subsequently, the second for loop is tasked with com ...

How do I retrieve the date from a dynamically generated text field within an ng-repeat loop and pass it

I have a specific issue with accessing the value of dynamically created input text in my web form. The problem arises when using ngRepeat to manage items, particularly with $scope.newValueItem being undefined. How can I retrieve the value from dynamically ...

Null value returned by getDOM() function during transition from Angular 2 to Angular 4

I've encountered a unique challenge that has me stumped. Despite finding similar issues, the proposed solutions don't seem to work for my case. My issue arose when I decided to migrate a project from Angular 2 to Angular 4. I began by creating a ...

Maintain the organization of folders during compilation using grunt

Currently, I am using grunt to convert jade files into HTML files. The structure of my files is as follows: index.jade |--partials/ view1.jade view2.jade To compile these files, I am utilizing grunt-contrib-jade. Below is the code ...

Remove duplicate identifiers from a list containing multiple arrays

How can I remove duplicate assets from the list? Please refer to the image for clarification. I attempted to use map and filter functions, but encountered an issue where additional attributes were lost. The desired outcome is to maintain the structure whi ...

I'm curious if anyone has had success utilizing react-testing-library to effectively test change events on a draftJS Editor component

​I'm having trouble with the fireEvent.change() method. When I try to use it, I get an error saying there are no setters on the element. After that, I attempted using aria selectors instead. const DraftEditor = getByRole('textbox') Draf ...

The external JavaScript is not functioning properly, causing the failure of the element function

I'm new to learning JS and I'm struggling to get a function to run successfully using external javascript. I can't figure out what's going wrong. The strange thing is that it works fine with inline javascript, but not with external. Bel ...

Transforming a "singular or multiple" array into an array of arrays using TypeScript

What is causing the compilation error in the following code snippet, and how can it be resolved: function f(x: string[] | string[][]): string[][] { return Array.isArray(x[0]) ? x : [x]; } Upon inspection, it appears that the return value will constantly ...

Applying a class to an element in VueJS is not functioning as expected

My goal is to assign the class .testcolor to the div element when testvalue is true, and apply no class when it's false. I encountered an issue where the getClass method does not get called when added to :class attribute, but works fine when called f ...

Exporting modules/namespaces to the window object in TypeScript

I have experience building APIs and applications in ES2015, but I am still learning the best practices for TypeScript. Can someone assist me with this challenge? Suppose I am creating an API/SDK for a shop. The objective is for the user to include my js f ...

The z-index overlapping problem in webkit browsers is a result of Angular 7 animations

I have implemented staggered animations in my Angular 7 application to bring elements to life upon page load. However, I am facing a strange z-index problem with one of my components. Here is the animation code: @Component({ selector: 'track-page& ...

"Authentic JavaScript Universal Time Coordinated (UTC) Date

I've been struggling to keep my dates in UTC within my JavaScript application. Surprisingly, the Date's getTimezoneOffset() method does not return a value of 0, which I expected it to do. This seems crucial for accurately converting dates between ...

Using BeautifulSoup to extract data from a webpage containing JavaScript

Hello everyone! I am reaching out for help once more. While I am comfortable scraping simple websites with tags, I recently came across a more complex website that includes JavaScript. Specifically, I am looking to extract all the estimates located at the ...

The uncertainty lies in determining the base address of an array

When looking at the following code, does cc hold the base address of the array c or does it actually hold the address of the letter 'H' from "Hello"? And if it is supposed to be holding two pointers, why doesn't cc get incremented by 8 bytes ...

What is the best way to retrieve the IDs of selected items in jQuery selectables?

I have a straightforward question that I've been struggling to find an answer to. When using jQuery selectables, I want to retrieve the ID of the selected list item when it's clicked. Here is an example of my list: <ol id="selectable"> ...

What is the process for transferring an image from the front end to the back end?

Does anyone know the best method for transferring an image from the frontend (using ANGULARJS) to the backend (Java) through a g Ajax call? I want to save this image on a local server, utilizing streams. Any suggestions or tips would be greatly appreciate ...

Error in Passport JS: Trying to use an undefined function

I've been struggling with debugging my code in Express and Passport. I've tried following solutions from others but can't seem to get it right. Any help or useful links would be greatly appreciated. Here is the error message along with the ...

What is the impact on resources when sending an AJAX request within a setTimeout() function?

I am in the process of developing a live chat system for my website, but I am uncertain about the best approach to take. Initially, I planned on using an AJAX request to retrieve the chat log at set intervals, which seems to be a common method based on var ...

Transfer of double array in Emscripten - A seemingly arbitrary address has been obtained

I am facing a challenge in passing a double array to a WASM file that is generated through emscripten. I have created it as a .js output file with WASM=1, which also gives me a wasm file. Below is an excerpt from my C++ code: #include <iostream> usi ...