1

I am working through a little practice assignment and have come across this question and for the life of me can't figure it out. There are tests parameters that I can't see. The object is not a variable I can see, it's just assumed.

Write a function called removePassword that takes in an object. Delete the property password and return the object.

removePassword=(object)=>{
for(var key in object){
  if(object = object[key]){
  delete object[key]
    }
  }return object;
}

I have tried a bunch of different versions of this code, but I don't know how to just delete the property password only without deleting the other property which is a username

1
  • object = object[key] you are assigning. It says to lool for 'password' you do not look for password. Commented Apr 8, 2021 at 20:53

3 Answers 3

2

Take a look at this solution. You can avoid doing the object copy if you want, it'll work anyway

const removePassword = (user) => {
  const userCopy = {...user} // CREATE A COPY OF THE OBJECT
  delete userCopy.password // DELETE THE PASSWORD PROPERTY
  return userCopy // RETURN UPDATED USER
}

const testUser = {username: 'Mario', password: 'supersecret123'}

console.log(
  removePassword(testUser)
)

Sign up to request clarification or add additional context in comments.

Comments

1

Could it work for you?

removePassword = (object) => {
  delete object.password;
  return object;
}

1 Comment

This is one of those times, where I thought I had tried that, but I guess I was doing it wrong, That works!! Thanks
1

You can see here link

That you can do it simply delete object.password or delete object["password"] :

const removePassword = (object) => {
  delete object.password;
  return object;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.