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

195
Visualizações
Deleting an object property with a function

Hey everyone İ am trying to write a function that accepts property name as an argument and deletes it. But i want a do it dynamically so i can change it anytime i want with other properties but i cant seem to make it. Help is much appreciated.

const student = {
  names: "David Rayy",
  sclass: "VI",
  rollno: 12,

  deleteProperty(property) {
    console.log(property)
    delete property
    console.log(student)
  },
}

student.deleteProperty(this.rollno)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

like that ?

const student =
  { names  : 'David Rayy'
  , sclass : 'VI'
  , rollno : 12
  , deleteProperty(property) 
    {
    console.log('delete ->', property)
    delete this[property]
    console.log(this)
  }
}

student.deleteProperty('rollno')

about 4 years ago · Juan Pablo Isaza Relatório

0

You should be passing in a string as an argument to the function so that it can be used as the property name.

const student = {

  name: 'David Rayy',
  sclass: 'VI',
  rollno: 12,

  deleteProperty(property) {
    delete this[property];
  }

}

student.deleteProperty('rollno');

console.log(`
  Name: ${student.name},
  Class: ${student.sclass},
  Rollno: ${student.rollno}
`);

Modern JS veers towards using classes. So an alternative solution might be to create a Student class that accepts some arguments, and has a deleteProperty method. Then create a new student instance with that data, and then you can delete the property you want.

class Student {

  constructor(name, sClass, rollNo) {
    this.name = name;
    this.sClass = sClass;
    this.rollNo = rollNo;
  }

  deleteProperty(property) {
    delete this[property];
    return this;
  }

  print() {
    console.log(this);
  }

}

const student = new Student('David', 'VI', 12);

student.print();
student.deleteProperty('rollNo').print();

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