0

Is There a way i ask user to enter a string, then I use the string to delete a property from object? like age, gender etc.

var person = {
  name: ['Bob', 'Smith'],
  age: 32,
  gender: 'male',
  interests: ['music', 'skiing'],
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
parisadz
  • 21
  • 9

1 Answers1

2

You can use the delete operator

var person = {
  name: ['Bob', 'Smith'],
  age: 32,
  gender: 'male',
  interests: ['music', 'skiing'],
}

const propertyToDelete = prompt("Property to delete ?")

delete person[propertyToDelete]

console.log(person)
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
  • They said that the property name is a string given by the user, so you need to use dynamic property access. – Barmar Aug 09 '22 at 07:05