如何在 TypeScript 和 Angular 中的 Record<string, Object[]> 上使用 foreach?

Roo*_*mey 4 c# typescript angular

我想知道如何在 TypeScript 中重新创建此 C# 代码

            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();
            }
Run Code Online (Sandbox Code Playgroud)

我希望我会进入控制台:

11
a
b
c

22
a2
b2
c2
Run Code Online (Sandbox Code Playgroud)

更新

最重要的是,我如何在 Angular 中使用这样的代码以及为什么我的 ngOnInit 方法未定义?

我的回答课

import { Question } from './question';

export class Answer {
    AnswerId: number;
    Content: string;
    IsCorrect: boolean;
    Mark: number;
    QuestionId: number;
    Question: Question;
}
Run Code Online (Sandbox Code Playgroud)

元件代码

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()
                    }
                });
    }

Run Code Online (Sandbox Code Playgroud)

当我尝试输出 s.Content 时,我得到了未定义。当我尝试使用 Angular 在页面上输出它时,我什么也没得到。

            <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>
Run Code Online (Sandbox Code Playgroud)

Rub*_*unk 13

这可能是对打字稿最直接的“翻译”。

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()
}
Run Code Online (Sandbox Code Playgroud)