Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
How to filter from constructor function the array in it?

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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

filter doesn't modify the array, it returns a new one. this.subjects = this.subjects.filter plus a change in how you filter:

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 = this.subjects.filter(function(current) {
        return current !== remove
    })
}

const student1 = new Student(123,'pepe')

student1.addSubject('a');
student1.addSubject('Math');
student1.addSubject('b');
student1.addSubject('c');
console.log(student1.subjects);
student1.removeSubject('Math');
console.log(student1.subjects)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .push() and .splice() to modify the subjects array in-place, instead of re-creating it for every modification, which a) is a waste and b) can cause difficult-to-find bugs.

But you absolutely should create a new array at the point of object creation, to avoid inadvertently modifying the subjects parameter outside during the lifetime of the Student instance.

function Student(id, name, subjects = []) {
  this.id = id;
  this.name = name;
  if (!Array.isArray(subjects)) throw new Error("subjects must be an array");
  this.subjects = [...subjects];
}

Student.prototype.addSubject = function (subject) {
  this.subjects.push(subject);   
}

Student.prototype.removeSubject = function (subject) {
  const pos = this.subjects.indexOf(subject);
  if (pos > -1) this.subjects.splice(pos, 1);
}

const student1 = new Student(1, 'Reed');

student1.addSubject('Math');
console.log(student1.subjects);
student1.removeSubject('Math');
console.log(student1.subjects)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda