Connect individual qualities of a diverse object (including embedded objects) to an array of linked lists

I'm currently working on a project where I need to dynamically derive a table column structure from any type of generic object. This object may contain properties of various types, and each property that is not an object itself should be considered a separate column in the table. If a property happens to be another object, then its properties should also translate into columns (and so forth).

For example, let's take a look at the following object.

export interface Event {
  name: string;
  meta: {
    created: Date,
    updated: Date,
  },
  some_data: {
    nested_data: {
      some_nested_data: string;
    }
  }
}

The goal is to display a list of these objects in a structured table format as shown below.

name created updated some_nested_data
Bob 1/1/2020 2/1/2020 some value
Anna 1/2/2020 2/2/2020 some value

Note: The objective is to make this work seamlessly for any type of generic object.

Approach Attempted

To tackle this challenge, I have introduced a Column class.

export class Column extends LinkedList<ObjectProperty>

This class is complemented by the following code snippet.

export type ObjectProperty = { name: string, type: string }

export interface ILinkedList<T> {
  insertInBegin(data: T): Node<T>;
  insertAtEnd(data: T): Node<T>;
  deleteNode(node: Node<T>): void;
  traverse(): T[];
  size(): number;
  search(comparator: (data: T) => boolean): Node<T> | null;
}

export class Node<T> {
  public next: Node<T> | null = null;
  public prev: Node<T> | null = null;
  constructor(public data: T) {}
}

By utilizing LinkedList<ObjectProperty>, I can access any object property value within a column by providing an array of property names (using the traverse() function to retrieve the array) and navigating through the object until reaching the desired value.

With this setup, I believe it is possible to display the data in the desired tabular form using the following methodology.

<thead>
  <tr>
    <ng-container *ngFor="let column of columns">
      <th *ngFor="let property of column.traverse(); let i = index">
        {{ column.getLast().data.name }}
      </th>
    </ng-container>
  </tr>
</thead>
<tbody>
  <tr *ngFor="let object of dataSorted; let i = index">
    <td *ngFor="let column of columns">
      {{ getColumnValue(column, object) }}
    </td>
  </tr>
</tbody>

Challenge Faced

I find myself struggling when it comes to mapping an object's keys to an array of Columns containing a linked list populated with object properties.

I have experimented with different functions involving Object.keys(object).map() and multiple recursive approaches, but I am unable to arrive at a concrete solution, especially considering the support needed for nested objects.

In essence: My aim is to correlate a generic object's properties to an array of Column objects (refer to Column class).

Any guidance or assistance in solving this matter would be greatly appreciated!

Please feel free to request additional information or resources if necessary.

Answer №1

It appears that the solution may be simpler than initially thought, as you just need a basic array. From what I gather, you have a generic component for displaying an array of items. In this scenario, you can leverage the keyvalue pipe offered by Angular : https://angular.io/api/common/KeyValuePipe

component.ts :

export class MyComponent {
   items = [
        { id: 0, name: "foo" },
        { id: 1, name: "bar" },
    ];
    items2 = [
        { firstName: "John", lastName: "Doe" },
        { firstName: "James", lastName: "Bond" },
    ];

    keys = Object.keys(this.items[0]);
    keys2 = Object.keys(this.items2[0]);

}

In your component.html :

<table>
    <thead>
        <tr>
            <th *ngFor="let key of keys">{{key}}</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of items">
            <td *ngFor="let keyValue of item | keyvalue">{{keyValue.value}}</td>
        </tr>
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th *ngFor="let key of keys2">{{key}}</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of items2">
            <td *ngFor="let keyValue of item | keyvalue">{{keyValue.value}}</td>
        </tr>
    </tbody>
</table>

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

Ways to include various inputs with chip

I am currently working on a project that involves implementing an email field using the chip component. However, I have encountered an issue where pasting multiple email values for the first time inserts them into the field successfully. But when I try to ...

The function `Object.entries().map()` in TypeScript does not retain the original data types. What changes can I make to my interface to ensure it works correctly, or is there a workaround

Working with this interface: export interface NPMPackage { name: string; description: string; 'dist-tags': { [tag: string]: string; }; versions: { [version: string]: { name: string; version: string; dependencie ...

Differences between Arrays of Numbers and Objects in Typegoose

Exploring Typegoose and encountering a puzzling issue. I defined a class with a field meant to store an array of numbers like this: class A { ... some other properties ... @prop({ required: true }) public nums!: number[]; } Here's a snippet of ...

How can modifications be made to HTML strings passed through props in React?

I'm working on implementing a pre-loader icon before an image is rendered in a React component using the code snippet below: export default class MediaPostItem extends BaseComponent { public constructor(props: IMediaPostItemProperties) { ...

When attempting to add mp3 files to a Vue/TypeScript application, a "Module not found" error is triggered

I am encountering an error while trying to import .mp3 files into my Vue/Typescript app. Upon running 'npm serve', I am presented with the following message: ERROR in /Users/***/***/application/src/components/Sampler.vue(80,16): 80:16 Cannot fin ...

Unable to retrieve the value from the nested formGroup

I am currently in the process of setting up nested formGroup fields using HTML code. <form [formGroup]="userProfileForm" (ngSubmit)="bookUser()" class="form"> <!-- userName --> <div class="form-group"> <label for="user ...

Tips on transferring data to a parent component without dismissing the Angular Material dialog when a button is clicked

Is there a way to transfer data from a popup (angular material dialog) to its parent component when a button is clicked, all without closing the popup window? I'm stumped on how to accomplish this task. If anyone knows a solution, please lend me a ha ...

angular 5 offers the ability to display and conceal elements with ease

<div class="m-t-sm"> <app-button [btnText]="'ADD USER'" (click)="!show" [btnType]="'text'" [btnColor]='"submit-btn-color"'></app-button> </div> Once the "add user" button is clicked, the following div ...

The act of updating Angular 2 to Angular 4 has resulted in an issue where the 'AnimationDriver' member is not being exported

After upgrading my Angular 2 to Angular 4, I encountered an error message when running my project: The module 'node_modules/@angular/platform-browser/platform-browser' does not have the exported member 'AnimationDriver'. ...

Retrieve information from the API following a change in language with Ngx-Translate

I am working on a web app using Angular 12 that supports 3 languages. To manage the languages, I have utilized Ngx-Translate. Overall, everything is functioning correctly, but I have encountered a small issue. I am using the onLangChange event emitter to ...

Error Uncovered: Ionic 2 Singleton Service Experiencing Issues

I have developed a User class to be used as a singleton service in multiple components. Could you please review if the Injectable() declaration is correct? import { Injectable } from '@angular/core'; import {Http, Headers} from '@angular/ht ...

Find non-null values inferred from a string identifier in Typescript

Imagine having a dynamic object with various keys and values, where the keys can be of any type, including null and undefined. Now, consider a function that takes an array of string paths to values within this object and returns true only if all those val ...

Troubleshooting in Visual Studio Code using an npm script

Within my package.json file, I have defined some scripts as shown below: "scripts": { "build": "tsc -p tsconfig.json", "run": "node --experimental-specifier-resolution=node .", "start" ...

Leveraging foreign key attributes within Angular templates

My technology stack includes Django for the backend with Django Rest Framework and Angular for the frontend. Within the backend, I have defined 2 models: class Post(models.Model): category = models.ForeignKey(Category, on_delete=models.SET_NULL, null= ...

I need help differentiating "namespace" from "static attributes" in TypeScript

Separating namespace from static properties in TypeScript can sometimes be tricky. error: 'ClassA' only refers to a type, but is being used as a namespace here. class ClassA { static ClassB = class { }; } type T = ClassA // ok type T = C ...

All you need to know about the impressive world of HTML5

When the server sends coordinates for a shape like a rectangle, rhombus, circle, trapezium, or polygon and I am uncertain about which one will be received, how should I decide on the figure to draw on the canvas using the Angular framework? ...

Encountered an issue while resolving symbol values statically within my exclusive set of modules

Encountered an error while resolving symbol values statically. The function 'DataModule' is not supported. Consider using a reference to an exported function instead of a function or lambda, resolving the symbol DataModuleRoot in E:/shopify-clien ...

Pass a method parameter in the subscribe function in Angular

I am looking to utilize the subscribe method within a function, in which I pass my variable (cities) as a parameter. export class MyComponent implements OnInit { cities:any; constructor(private myApiService: MyApiService) { myMethod(this.cities); ...

How is it that I can assign a string value to my declared number object parameter in Typescript?

In my code, I have defined an interface called DateArray: export interface DateArray { year : number; } Within my component, I am declaring a new variable named dateArray like this: private dateArray: DateArray = { year: null }; Then, I am a ...

Implement Angular backend API on Azure Cloud Platform

I successfully created a backend API that connects to SQL and is hosted on my Azure account. However, I am unsure of the steps needed to deploy this API on Azure and make it accessible so that I can connect my Angular app to its URL instead of using loca ...