I want to know, how can I recreate this C# code in TypeScript
Dictionary<string, List<string>> sas = new Dictionary<string, List<string>>();
sas.Add("11", new List<string> { "a", "b", "c" });
sas.Add("22", new List<string> { "a2", "b2", "c2" });
foreach(var e in sas)
{
Console.WriteLine(e.Key);
foreach(var s in e.Value)
{
Console.WriteLine(s);
}
Console.WriteLine();
}
I expect that I will get in console this:
11
a
b
c
22
a2
b2
c2
Update
And the most important, how can I use code like this in Angular and why I get undefined un my ngOnInit method?
My Answer class
import { Question } from './question';
export class Answer {
AnswerId: number;
Content: string;
IsCorrect: boolean;
Mark: number;
QuestionId: number;
Question: Question;
}
Component code
testInfo: Record<string, Answer[]>;
ngOnInit() {
if (this.id)
this.dataService.getTestStart(this.id)
.subscribe((data: Record<string, Answer[]>) =>
{
this.testInfo = data; this.loaded = true;
console.log(this.testInfo);
for (const key in this.testInfo) {
console.log(key);
for (const s of this.testInfo[key]) {
console.log(s.Content)//here is undefined
}
console.log()
}
});
}
When I try to output s.Content i get undefined. And when I try to output this on page using Angular I got nothing.
<tr *ngFor="let key of testInfo">
<td class="row-number-column">{{key}}</td>
<template *ngFor="let s of testInfo[key]">
<td>
{{s?.content}}
</td>
</template>
</tr>
This is probably the most direct 'translation' to typescript.
const sas: Record<string, string[]> = {}
sas["11"] = ["a", "b", "c"]
sas["22"] = ["a2", "b2", "c2"]
for (const key in sas) {
console.log(key)
for (const s of sas[key]) {
console.log(s)
}
console.log()
}
Typescript:
const sas: Record<string, string[]> = {};
sas['11'] = ['a', 'b', 'c'];
sas['22'] = ['a2', 'b2', 'c2'];
for (const key in sas) {
if (sas.hasOwnProperty(key)) {
console.log(key)
for (const key2 in sas[key]) {
if (sas[key].hasOwnProperty(key2)) {
console.log(sas[key][key2]);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With