In order to showcase the data from the second JSON by using the unique identifier

SCENARIO:

I currently have two JSON files named contacts and workers:

contacts

[
	{
		"name": "Jhon Doe",
		"gender": "Male",
		"workers": [
			"e39f9302-77b3-4c52-a858-adb67651ce86",
			"38688c41-8fda-41d7-b0f5-c37dce3f5374"
		]
	},
	{
		"name": "Peter Parker",
		"gender": "Male",
		"workers": [
			"e39f9302-77b3-4c52-a858-adb67651ce86",
			"40665c50-ff74-4e81-b968-e127bdf1fe28"
		]
	},
	{
		"name": "Mark Wood",
		"gender": "Male",
		"workers": ["ed780d15-428b-4bcd-8a91-bacae8b0b72e"]
	},
	{
		"name": "Mary Jane",
		"gender": "Female",
		"workers": [
			"40665c50-ff74-4e81-b968-e127bdf1fe28",
			"ed780d15-428b-4bcd-8a91-bacae8b0b72e"
		]
	}
]

workers

[
	{
		"id": "e39f9302-77b3-4c52-a858-adb67651ce86",
		"name": "Alfy Odhams"
	},
	{
		"id": "38688c41-8fda-41d7-b0f5-c37dce3f5374",
		"name": "Allsun Suttle"
	},
	{
		"id": "ed780d15-428b-4bcd-8a91-bacae8b0b72e",
		"name": "Alvinia Ettritch"
	},
	{
		"id": "40665c50-ff74-4e81-b968-e127bdf1fe28",
		"name": "Ambrosi Lindenstrauss"
	}
]

I am presenting the contacts like this:

https://i.stack.imgur.com/VVoCe.png

EXPECTED RESULT :

I aim to exhibit the assigned worker's name for each contact based on their ID as shown here:

https://i.stack.imgur.com/JQIX9.jpg

Check out the DEMO on StackBlitz.

Answer №1

Here is the requested information

<tr>
    <td>Assigned Employees</td>
    <td>
        <div *ngFor="let eEmployee of employee.contacts">
            <div *ngFor="let employee of employees" [hidden]="eEmployee!=employee.id">
              {{employee.name}}
            </div>
        </div>
    </td>
</tr>

Answer №2

If you're looking for a way to find your worker's name based on their ID, you can utilize the Array.prototype.find() method in JavaScript. Simply create a function within your .ts file like this:

 findWorkerName(wid) {
    let obj = this.workers.find(({ id }) => id === wid);

    return obj ? obj.name : '';
}

In your component file, structure it as follows:

<h4>Contacts</h4>
<div class="cust-detail" *ngFor="let contact of contacts">
    <tr>
        <td>Name</td>
        <td>{{contact.name }}</td>
    </tr>
    <tr>
        <td>Gender</td>
        <td>{{contact.gender}} </td>
    </tr>
    <tr>
        <td>Assigned Workers</td>
        <td>
            <span *ngFor="let id of contact.workers let i = index">
            {{findWorkerName(id)}} {{i < contact.workers.length - 1 ? ',' : ''}}
          </span>
        </td>
      </tr>
             <hr>
  </div>

You can view a working demo of this code on StackBlitz. I have made some updates to your existing code.

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

Encountering a 404 error in Angular 9 when refreshing the browser

Encountering a 404 error when attempting to access mysite.com/menu directly, but no issues when navigating through the homepage and selecting the menu option. Tried implementing an .htaccess file following Angular documentation, yet problem persists. Cur ...

Developing a project using npm and Node.js with Typescript has been smooth sailing so far. However, an issue has arisen

Recently, I came across a helpful guide on setting up a Node.js project in Typescript on this website: https://basarat.gitbooks.io/typescript/docs/quick/nodejs.html The guide also provides instructions for auto-recompilation upon changes when using npm st ...

Tips for transferring data between two pop-up windows in Angular2

My Angular2 project has a specific requirement that involves opening a first popup for users to enter some values. Upon a user event, like clicking a button, the first popup should close and a second popup should open with the values pre-populated from the ...

JSON calls that can be made across different domains

Similar Question: Ajax cross domain call I am facing an issue with calling Asp.Net Controller methods that return JSON responses from a different domain. Even when using Jquery's $.getJSON(...){}, I am encountering difficulties. After some quick ...

What are the reasons behind the restriction on using non-public members in TypeScript classes?

Consider the following scenario: class Trait { publicMethod() { this.privateMethod(); // do something more } private privateMethod() { // perform a useful action } } When attempting to implement it like this: cla ...

Edge Runtime does not permit the use of Dynamic Code Evaluation functions such as 'eval', 'new Function', and 'WebAssembly.compile'

My project involves the implementation of NextUI components. I incorporated the Select component from NextUI, and during development mode, everything functioned flawlessly. However, upon completion of development and attempting to build the project, I enc ...

What is the Reason FormControl#valueChanges Subscriptions are Not Cleaned up by Garbage Collection?

After reading numerous discussions, I've learned that it's important to unsubscribe from `FormControl#valueChanges` in order to avoid memory leaks. I now understand the importance of knowing when and how to unsubscribe from Observables, especiall ...

Enabling CORS header 'Access-Control-Allow-Origin' in Angular 2 using Typescript

I am currently developing an Angular 2 typescript application on my localhost. I recently encountered an issue when trying to access an external REST API in my application, resulting in the following error message: Cross-Origin Request Blocked: The Same ...

Utilizing Next.js to Import a Function from a Path Stored within an Environment Variable

I'm having trouble importing a function whose path is stored in an environment variable Current import statement: import { debug } from '../lib/site-A/utils' Expected import statement: import { debug } from process.env.DEBUG_PATH In my ...

The Angular RXJS HTTPClient fails to properly handle HttpErrorResponses

In my Angular 12 project, I am facing an issue with setting up unit tests that should capture errors from HTTP responses. However, when running the tests, instead of receiving the error as an actual error object, it is being passed within the response body ...

What could be causing these Typescript compilation errors I am experiencing?

I am puzzled by the appearance of these errors, especially since I copied the entire ClientApp folder from a running application where they did not exist. Here is the structure of my project: This is my package.json file: { "name": "crossvertise-calan ...

I need help figuring out how to incorporate dynamic checkboxes in Angular

Currently, I am working on integrating dynamic forms into my Angular application and I am referring to the https://angular.io/guide/dynamic-form guide for guidance. Specifically, I have a checkbox question that consists of more than four options to choose ...

What is the reason behind prettier's insistence on prefixing my IIAFE with ";"?

I've encountered async functions in my useEffect hooks while working on a JavaScript project that I'm currently transitioning to TypeScript: (async ():Promise<void> => { const data = await fetchData() setData(data) })() Previously, ...

Can the return type of a function be determined dynamically at runtime by analyzing a function parameter value?

Let's delve into this concept with an illustrative example! interface Query { foo: any; } interface Mutation { bar: any; } type OperationName = keyof Query | keyof Mutation; const request = async <T>(args, operationName: OperationName): P ...

Angular2 - HTML not displaying the response

I am currently mastering angularjs2. In my latest project, I attempted to fetch data from an API and received a response successfully. However, I encountered an issue where the response is not rendering in homepage.component.html as expected. I am unsure o ...

Providing UI field attributes within server JSON data

Suppose I have numerous form fields that need to be displayed on the user interface. Additionally, in a standard Modify workflow, let's assume that these fields will have various attributes like value, mandatory, editable, disabled, label, regex (for ...

Steps to perform a task that relies on two observables

I'm currently working on a function that requires two responses from two separate asynchronous functions, both returning observables. 1. Func1 returns an observable 2. Func2 returns an observable These functions are independent and can be executed sep ...

Navigating to an external URL within a component in Angular 4: Tips and Tricks

Currently, I am working on a project using a node/express server along with Angular 4. To streamline my work with the endpoints I have created, I use the command ng build --watch --output-path. My project involves controlling music through the Spotify web ...

Show a Mat-Table with distinct values only

In my mat-table, I have three columns: part, type, and name. When I bind these columns with data from the service, I notice that the same data is being displayed in multiple rows, resulting in duplicate entries. I want to combine the data from all three co ...

Help needed with parsing nested JSON using the $.each function

Here is a JSON response sample that needs to be parsed in a more generic manner, rather than using transactionList.transaction[0]. "rateType": interestonly, "relationshipId": consumer, "sourceCode": null, "subType": null, "transactionList": { "transac ...