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

168
Visualizações
Loop through all the objects of a class in javascript

I have a class Student and a method eligibleForPlacements which checks if a student is eligible for placements. How do I iterate through all the objects and check if the student is eligible using the method?

I have created an array static allObj = new Array(); which stores all the object but it isn't working:

class Student {
  static noOfStudents = 0;
  static allObj = new Array();
  constructor(name1, age, phoneNumber, boardMarks) {
    this.name = name1,
      this.age = age,
      this.phoneNumber = phoneNumber,
      this.boardMarks = boardMarks
    Student.noOfStudents += 1;
    Student.allObj.push(this);
  }
  eligibleForPlacements(minMark) {
    return (age1) => {
      if (age1 < this.age && this.boardMarks > minMark) {
        console.log(`${this.name} is eligible for placements`);
      } else {
        console.log(`${this.name} is not eligible for placements`);
      }
    }
  }
}

//created two object and iterating through it

const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);

for (let i = 0; i < Student.allObj.length; i++) {
  Student[i].eligibleForPlacements(10)(5);
}

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

0

You are missing .allObj at the end

class Student {
  static noOfStudents = 0;
  static allObj = new Array();
  constructor(name1, age, phoneNumber, boardMarks) {
    this.name = name1,
      this.age = age,
      this.phoneNumber = phoneNumber,
      this.boardMarks = boardMarks
    Student.noOfStudents += 1;
    Student.allObj.push(this);
  }
  eligibleForPlacements(minMark) {
    return (age1) => {
      if (age1 < this.age && this.boardMarks > minMark) {
        console.log(`${this.name} is eligible for placements`);
      } else {
        console.log(`${this.name} is not eligible for placements`);
      }
    }
  }
}

const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);

for (let i = 0; i < Student.allObj.length; i++) {
  // add .allObj here
  Student.allObj[i].eligibleForPlacements(10)(5);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You're trying to read the index of the Student class itself, not the allObj property; change Student[i].eligibleForPlacements(10)(5); to Student.allObj[i].eligibleForPlacements(10)(5); and it works fine:

class Student {
  static noOfStudents = 0;
  static allObj = new Array();
  constructor(name1, age, phoneNumber, boardMarks) {
    this.name = name1,
      this.age = age,
      this.phoneNumber = phoneNumber,
      this.boardMarks = boardMarks
    Student.noOfStudents += 1;
    Student.allObj.push(this);
  }
  eligibleForPlacements(minMark) {
    return (age1) => {
      if (age1 < this.age && this.boardMarks > minMark) {
        console.log(`${this.name} is eligible for placements`);
      } else {
        console.log(`${this.name} is not eligible for placements`);
      }
    }
  }
}

const lili = new Student('Lili', 16, '2827384788', 50);
const ria = new Student('Ria', 23, '2827384788', 30);

for (let i = 0; i < Student.allObj.length; i++) {
  Student.allObj[i].eligibleForPlacements(10)(5);
}

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