我有以下使用免疫表的类型脚本。我希望使用免疫表列表来处理学生,并生成一个唯一的学生列表。我的初始列表可能有重复的student对象,我可以按studentId将它们过滤掉。
我面临的问题是,当迭代列表时,我无法从Student对象访问studentId属性,行中的studentId不可访问。
如果(d&&!studentIdeen.有(d!.studentId(
export interface StudentProperties {
studentId: number;
student: string;
region: string;
active: boolean;
}
export interface Student extends TypedRecord<Student>, StudentProperties { }
export const studentFactory = makeTypedFactory<StudentProperties, Student>({ studentId: -1, student: '', region: '', active: true });
REDUX CALL
case types.FETCH_STUDENT_DATA_SUCCESS: {
let fetchedStudents = List(action.data.map(item => studentFactory(item)));
console.log("Fetched student data ==> " + fetchedStudents);
let uniqueStudents: Student[] = [];
let studentIdSeen: Set<number>;
fetchedStudents.forEach(d => {
if (d && !studentIdSeen.has(d!.studentId) { //error here , not able to access studentId attribute
uniqueStudents.push(d);
studentIdSeen.add(d!.studentId);////error here , not able to access studentId attribute
}
});
console.log("List of unique students = " + uniqueStudents);
return state.set('students', List(uniqueStudents));
}
如果我返回state.set('students',fetchedStudents(中的fetchedStudents,它工作正常,所以正在提取的数据没有问题。
拳头:d!.studentId
是错的,你只想d.studentId
两者都在:!studentIdSeen.has(d!.studentId)
和:studentIdSeen.add(d!.studentId);
其次,如果可能的话,您可以强制转换为一个可以删除重复项的集合。然后返回阵列:
let uniqueStudents: Student[] = [... new Set(fetchedStudents)]
或
let uniqueStudents: Student[] = [];
fetchedStudents.forEach(d => {
if (d && !uniqueStudents.has(x => x.studentId === d.studentId)) {
uniqueStudents.push(d);
}
});