In order to retrieve specific object attributes using the unique identifier

I am currently managing 2 API's referred to as teachers and sessions.

The contents of the teachers JSON file are:

   [
      {
        "teacherName": "Binky Alderwick",
        "id": "01"
      },
      {
        "teacherName": "Basilio Gregg",
        "id": "02"
      },
      {
        "teacherName": "Binky Alderwick",
        "id": "03"
      },
      {
        "teacherName": "Cole Moxom",
        "id": "04"
      }
    ] 

The sessions JSON file looks like this:

[
    {
        "id":"001",
        "sessionName": "Chemistry",
        "notes": "Chemistry is the study of matter, its properties",
        "teacherId": "01"<==========
    },
    {
        "id":"002",
        "sessionName": "Physics",
        "notes": "Physics is the natural science that studies matter and its motion ",
        "teacherId": "03"
    },
    {
        "id":"003",
        "sessionName": "Maths",
        "notes": "Mathematics includes the study of such topics as quantity",
        "teacherId": "01"<=========

    },
    {
        "id":"004",
        "sessionName": "Biology",
       "notes": "Biology is the natural science that studies life and living organisms",
        "teacherId": "04"
    }
]

My goal is to showcase all the teachers in the template as shown in this:

https://i.sstatic.net/oH02B.png

Within the sessions JSON, the teacherID is mentioned. I aim to display the sessions specifically associated with the particular teacher based on their teachersID.

For example, since the sessions (Chemistry & Maths) have a teacherID of (01), I intend to display these 2 sessions (Chemistry & Maths) under Teacher 1 (Binky Alderwick) like so:

https://i.sstatic.net/1kzZx.png

I need to retrieve all the properties of the session object based on the teacherId.

Check out the Stackblitz DEMO here

Answer №1

To streamline the process, you can develop a single function that generates an array of sessions specifically for a chosen teacher.

For instance, in the app.component.ts file:

retrieveSessionsForTeacher(teacherId) {
    return this.sessions ? this.sessions.filter(x => x.teacherId === teacherId) : [];
}

HTML implementation:

<h4>Teachers</h4>
<div class="cust-detail" *ngFor="let teacher of teachers">
    <tr>
        <td>Name</td>
        <td>{{teacher.teacherName }}</td>
    </tr>
    <tr>
        <td>Sessions</td>
        <td><br>
      <div *ngFor="let item of retrieveSessionsForTeacher(teacher.id)">
      <h2>{{item.sessionName}}</h2>
      <p>{{item.notes}}</p>
      </div>
    </td>
    </tr>   
    <hr>
</div>

Here is a comprehensive demonstration using StackBlitz to address your query.

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

What steps should I take to fix this Angular 8 error?

Encountered an issue in ../node_modules/@angular/http/src/backends/jsonp_backend.d.ts:1:28 - error TS2307: Module 'rxjs/Observable' not found. 1 import { Observable } from 'rxjs/Observable'; ~~~~~~~~~ ...

Best practice in Angular 4: utilize services to load and store global data efficiently

I would like to create an angular 4 application that allows me to search for a user in a database and use their information across different routes. The issue I am facing currently is that when I load data via a service, change the route, and then return, ...

Angular 2.0.0 - Simulated File Loader

For my unit testing, I am facing the challenge of mocking the FileUploader class from ng2-file-upload. Can anyone guide me on how to accomplish this? import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; @Component({ selector: &apos ...

Creating subclass mappings and defining subclasses dynamically using Typescript at runtime

I am puzzled by the structure of 3 classes: A, B, and C. Both B and C extend the abstract class A. abstract class A { public name: string; constructor(name: string) { this.name = name; } abstract getName(): string; } class B extends A { g ...

Combining Django REST with Angular

Exploring the use of Angular (not AngularJS) in production has left me a bit confused. Different tutorials and explanations have presented two main approaches: The first approach involves setting up separate application servers, such as Apache Angula ...

Turning XSD into TypeScript code

Stumbling upon this tool called CXSD, I was intrigued. The documentation describes cxsd as a streaming XSD parser and XML parser generator designed for Node.js and TypeScript (optional but highly recommended). It seemed like the perfect solution for my ne ...

Angular array of considerable size

Dealing with a massive array of 30,000 items can be quite daunting, especially when it comes in as a stream using grpc-web. Currently, I'm fetching the data from grpc.client() and populating an array before displaying it using *ngFor. However, I' ...

Enhance the appearance of a custom checkbox component in Angular

I developed a customized toggle switch for my application and integrated it into various sections. Recently, I decided to rework it as a component. However, I am encountering an issue where the toggle switch button does not update in the view (it remains t ...

Adding a blank line in an Angular table using primeNG to display database information requires a specific method

In my Angular application, I am utilizing primeNG table to display data fetched from a database. The table comes with 'add' and 'delete' buttons for user interaction. To view the interface, click on this link: https://i.stack.imgur.com/ ...

What is the technique for highlighting the exact data point on the XY am4chart when it is clicked?

I have been searching high and low for a solution to my problem but haven't had any luck. I am working with a traditional XY am4chart that has hundreds of data points in the line series. What I want is for the chart to display a vertical line (or some ...

The error message displayed by Create React App states: "You cannot utilize JSX without the '--jsx' flag."

I need help with overcoming this particular issue in a TypeScript based React application: Encountering an error stating "Cannot use JSX unless the '--jsx' flag is provided" ...

What could be causing Angular to replace the original variable?

As a newcomer to Angular, I've been working on this code for hours now. Hopefully, it will all come together for someone out there who can make sense of it. export class QuizComponent implements OnInit { originalArray: IArray[] = []; tempArray: I ...

How can I create a universal "Add" button in Angular that can be used across all child components?

Currently, I am working on a straightforward application featuring a toolbar at the top of the screen. Within this toolbar, there is a + button designated for adding content. The functionality of this + button changes based on which component is currently ...

Angular resolver does not return anything if the outcome is an observable generated using the .asObservable() method

While working on my project, I wanted to simulate my http calls by using a BehaviorSubject along with Observable properties in my resolver service. However, I faced an issue and I am not sure why this code snippet is not functioning as expected: schedule- ...

Assigning a Value to DropDown Menu in ngFor Angular 5

Here is the Angular5 code that I am working with: <div class="section"> <h6 style="font-weight:bold">Select a Branch</h6> <select [(ngModel)]='Schedule.Branch'> <option *ngFor="let item of D ...

Should the PHP interface be exported to a Typescript interface, or should it be vice versa?

As I delve into Typescript, I find myself coding backend in PHP for my current contract. In recent projects, I have created Typescript interfaces for the AJAX responses generated by my backend code. This ensures clarity for the frontend developer, whether ...

Tips for integrating yarn add/npm install with monorepositories

I am attempting to retrieve a node package from a private monorepo on GitHub, structured similarly to this: monorepoProject --- subProjectA --- subProjectB Both subProjectA and subProjectB are TypeScript projects, following the layout depicted below: ...

Updating a null value within the database was done successfully

Currently, I am working with angular CLI version 8.1.0 and have a user list displayed on a mat table. Upon clicking on a specific user, a new page opens up containing two buttons - "approve" and "reject". The issue I am facing is that when I click on "ap ...

Struggling to send information to the data layer on every page in Angular 9

Currently, I am in the process of integrating a GTM snippet into my Angular project. However, I have noticed that when the page is hard reloaded, it pushes data but does not do so during normal navigation. I have already added the GTM snippet provided by ...

Functional interceptor causing Router.navigate() to malfunction

This is a custom Angular interceptor designed to handle potential session timeouts during the usage of an application. export const sessionExpiredInterceptor: HttpInterceptorFn = (req, next) => { const router: Router = inject(Router); return nex ...