Recently, I embarked on my journey with Angular 2 and TypeScript.
My goal is to display data in a table. Specifically, I want to showcase one of the values as a name instead of an Id (a key) referencing another table.
In my quest for a solution, I stumbled upon a helpful thread on Stack Overflow discussing how to achieve this in TypeScript: Querying Typescript array collection based on key.
I followed the first answer provided and implemented it in my HTML component:
<div class="col-md-1">{{codeTypes.find(c => c.codeTypeId == level.codeType)[0].name}}</div>
However, I encountered a syntax error involving {{
. Removing {{
and }}
resulted in only displaying the code snippet in the HTML:
codeTypes.find(c => c.codeTypeId == level.codeType)[0].name
The codeTypes
variable is a public property within my TypeScript class:
public codeTypes: ICodeType[];
The interface ICodeType
is defined as follows:
export interface ICodeType {
codeTypeId: number;
name: string;
}
My objective is to query the codeTypes
array, find the matching codeTypeId
, and display its corresponding name
in the HTML component. How can I achieve this?