【发布时间】:2022-01-04 07:27:44
【问题描述】:
这是将预先写入的数组中的单个项目添加到表中的示例代码。只有一个名称元素,所以我只需要一个“
let people: string [] = ["Jack", "Michael"]
for (let i: number = 0; i < people.length; i++) {
document.getElementById ("buddies").innerHTML +=
`<tr>
<td>${people[i]}
<tr>`;
}
如果我想使用一个类而不是简单地将内容写入数组,我将如何获得相同的结果?
class = Person{
public FirstName : string
public LastName : string
constructor (Firstname: string, Lastname: string) {
this.FirstName = Firstname;
this.LastName = Lastname;}
let people: Person [] = [
new Person ("Peter", "Parker")]
for (let i: number = 0; i < people.length; i++) {
document.getElementById ("table").innerHTML +=
`<tr>
<td>${people[i]} //this is where the first name should be//
<td>${people[i]} //this is where the last name should be //
<tr>`;
}
我知道我的
我们将不胜感激。
【问题讨论】:
标签: arrays typescript function class