简体   繁体   中英

How do you replace an Object property in Javascript?

I've been stuck on this problem for awhile, and after various attepts, I decided it was time to ask for some help.

Here's the question: Create a function called changeEmail that takes in a user object and a newEmail string. Replace the user's current email address (assigned to the email property) with the newEmail string, then return the updated user object.

Here's my code

 var user = { name: "John Doe", email: "johndoe@gmail.com" }; function changeEmail(param1) { param1 = param1.email.replace("johndoe", "newjohndoe"); user.email = param1; return user; } changeEmail(user); console.log(user); 

To comply with the request

Create a function called changeEmail that takes in a user object and a newEmail string

your changeEmail function signature should look like

function changeEmail(userObject, emailString) {

}

Replace the user's current email address (assigned to the email property) with the newEmail string

means your function body could then look something like:

userObject.email = emailString;

and finally

then return the updated user object

would be

return userObject;

What's param1 ? You just have to do a replace on object attribute.

var user = {name: "John Doe", email: "johndoe@gmail.com"};

// If you want to replace a part of email by another thing:
user.email = user.email.replace("johndoe", "newjohndoe")

// If you just want to setup a new email:
user.email = "newjohndoe@gmail.com

Or with a function:

function replaceObjectParam(obj, key, old_value, new_value) {
  obj[key] = obj[key].replace(old_value, new_value)
  return obj
}
var user = {name: "John Doe", email: "johndoe@gmail.com"};
user = replaceObjectParam(user, 'email, "johndoe", "newjohndoe")

I think youre overcomplicating it. You just need to update the property:

function changeEmail(user,email){
 user.email = email;
 return user;
}

So you can do:

changeEmail({email:"before"},"after");

If you wanna annoy your teacher with some ESnext:

const changeEmail = (user,email)=>({...user,email});
//(note this does shallow copy)
const user = changeEmail({email:"before"},"after");

And actually, i think this assignment isnt really useful. Why not simply:

user.email = "after";

您应该更改属性而不是参数本身:

 param1.email = param1.email.replace("johndoe", "newjohndoe");

Let's look at your question again.

Create a function called changeEmail that takes in a user object and a newEmail string . Replace the user's current email address (assigned to the email property) with the newEmail string, then return the updated user object.

When looking at your code, the very first thing to notice is that your function is only accepting one param, not two. This is why it will not satisfy your question. Another note is that you want to usually name your params in a more descriptive way, so it makes sense when you're using them inside your function .

Your updated code might look like this.

 var user = {name: "John Doe", email: "johndoe@gmail.com"}; function changeEmail (userObj, newEmail) { userObj.email = newEmail return userObj; } changeEmail(user, "newemail@gmail.com"); 

You are returning all the wrong stuff in your example. Take a look at this,

var oldUser = new User("John", "foo@barr.com");  // Assuming you have a User object

function changeEmail(user, newEmail) {
    var newUser = new User();
    newUser.name = user.name;
    newUser.email = newEmail;
    return newUser;
}

var updatedUser = changeEmail(oldUser, "barr@foo.com");

You first have the old user to use, then you have the function to change their email.

When you want to change their email, you pass in the user object and a new email. The new user is now in updatedUser

A user object can be something simple like this,

var User = function(name, email) {
    this.name = name;
    this.email = email;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM