访问要在Angular中映射的对象的属性?

时间:2017-12-15 22:18:46

标签: reactjs

我正在尝试列出对象列表的所有特定属性。这是我的代码

const nameList = this.props.students.map((student, i) =>
    <div>{student.firstName}</div>
);

firstName是学生的财产。 students对象作为数组传递给组件,在组件界面中它看起来像这样:

interface StudentDetails {
    students: IStudent[]
}

这就是IStudent的样子:

interface IStudent {
    firstName: string;
    lastName: string;
    major: string;
}

我认为问题出在地图功能中。目标只是打印出学生名字列表。现在它只是空白

1 个答案:

答案 0 :(得分:0)

我怀疑this.props.students不是数组。如果它是一个数组,该代码将起作用。运行以下示例。

const students = [
  { firstName: "Tim", lastName: "Burton" },
  { firstName: "Jacob", lastName: "McNealy" }
];

const nameList = students.map(student => student.firstName);

console.log(nameList);

相关问题