最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do you replace an Object value in Javascript? - Stack Overflow

programmeradmin2浏览0评论

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: "[email protected]"
};

function changeEmail(param1) {
  param1 = param1.email.replace("johndoe", "newjohndoe");
  user.email = param1;
  return user;

}

changeEmail(user);
console.log(user);

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: "[email protected]"
};

function changeEmail(param1) {
  param1 = param1.email.replace("johndoe", "newjohndoe");
  user.email = param1;
  return user;

}

changeEmail(user);
console.log(user);

Share Improve this question edited Aug 20, 2022 at 17:55 MTK 3,5805 gold badges38 silver badges59 bronze badges asked Aug 21, 2017 at 15:12 JohnDoe99JohnDoe99 1092 gold badges2 silver badges4 bronze badges 2
  • 2 Your assignment itself is fine. Focus on "a function that takes in a user object" and "then returns the updated user object". – Bergi Commented Aug 21, 2017 at 15:16
  • The code in question works perfectly. What's the issue? – Sagar V Commented Aug 21, 2017 at 15:23
Add a ment  | 

6 Answers 6

Reset to default 3

To ply 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;

I think youre overplicating 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";

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

var user = {name: "John Doe", email: "[email protected]"};

// 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 = "[email protected]

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: "[email protected]"};
user = replaceObjectParam(user, 'email, "johndoe", "newjohndoe")

You should change the property not the parameter itself:

 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: "[email protected]"};

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

changeEmail(user, "[email protected]");

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

var oldUser = new User("John", "[email protected]");  // 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, "[email protected]");

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;
}
发布评论

评论列表(0)

  1. 暂无评论