[英]Loop through custom object array in TypeScript
I started playing a little bit with TypeScript. 我开始玩TypeScript。 I have created two classes (Student and Listview).
我创建了两个类(Student和Listview)。 I am trying to loop over the array with the student objects I created but somehow it doesn't work.
我正在尝试用我创建的学生对象遍历数组,但是某种程度上它不起作用。
class Student {
fullName: string;
constructor(public firstName, public middleInitial, public lastName) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
class Listview {
items: Array<Student>;
constructor(public item_list: Array<Student>) {}
log(): void {
var items = this.items;
for(var i=0; i<items.length; i++) {
console.log(items[i]);
}
}
}
var list = new Listview(
[new Student("Jane", "M.", "User"),
new Student("Hans", "M.", "Muster"),
new Student("Fritz", "B.", "Muster")]
);
list.log();
I get this warning in the console: 我在控制台中收到此警告:
How do I need to access the array to read the properties of each student object? 我如何访问数组以读取每个学生对象的属性?
Greetings Orkun 问候Orkun
Your ListView
should look like this, in order to correctly initialize items
: 您的
ListView
应该看起来像这样,以便正确初始化items
:
class Listview {
constructor(public items: Array<Student>) {}
log(): void {
var items = this.items;
for(var i=0; i<items.length; i++) {
console.log(items[i]);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.