I'm making a javascript course and I'm trying to create a function that filters from the array and removes a given value of it. NOTE: I've to use the filter() method.
I've tried several ways but without success. What am I doing wrong?
Here the code:
function Student(id, name, subjects = []) {
this.id = id;
this.name = name;
this.subjects = subjects;
}
Student.prototype.addSubject = function(subject) {
this.subjects = [...this.subjects, subject];
}
Student.prototype.removeSubject = function(remove) {
this.subjects.filter(function(remove) {
return this.subjects !== remove
})
}
const student1 = new Student(1, 'Reed');
student1.addSubject('Math');
console.log(student1.subjects);
student1.removeSubject('Math');
console.log(student1.subjects)
Appreciate your time
filterdoesn't modify the array, it returns a new one.this.subjects = this.subjects.filter(...)would fix the problemthis.subjects = this.subjects.filter(function(remove)...but still get['Math]whenconsole.log