Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda