函数正确返回,但是当我设置obj的参数时,obj什么都不返回?

时间:2019-11-02 18:48:45

标签: javascript

问题是database.studentMap返回Map(0){}

mapper(student)返回我想要的内容,所以问题出在我想的数据库中。

学生和课程是包含“学生”和“课程”对象的数组。

class Student {
    constructor(id, name, gpa, schedule) {
        this.id = id
        this.name = name
        this.gpa = gpa
        this.schedule = schedule
    }
}

class Database {
    constructor(studentMap, courseMap) {
        this.studentMap = studentMap
        this.courseMap = courseMap

    }

}
function mapper(arr) {
    let map = new Map()
    arr.forEach(obj => {
        map.set(obj[Object.keys(obj)[0]], obj)

    });
    return map
}

let students = []

fetch("https://maeyler.github.io/JS/data/Students.txt")
    .then(r => r.text())
    .then(text => text.split('\n'))
    .then(lines => {
        lines.forEach(line => {
            let blocks = line.split('\t')
            students.push(new Student(blocks[0], blocks[1], blocks[2], blocks.slice(3)))
        });
    })
// same for courses

let database = new Database(mapper(students), mapper(courses))

输出:

database.studentMap
->Map(0) {}
mapper(students)
->Map(3129) {"116690070" => Student, "116690190" => Student, ....}

0 个答案:

没有答案