简体   繁体   English

如何替换Javascript中的Object属性?

[英]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. 问题是:创建一个名为changeEmail的函数,该函数接受一个用户对象和一个newEmail字符串。 Replace the user's current email address (assigned to the email property) with the newEmail string, then return the updated user object. 用newEmail字符串替换用户的当前电子邮件地址(分配给email属性),然后返回更新的用户对象。

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 创建一个名为changeEmail的函数,该函数接受一个用户对象和一个newEmail字符串

your changeEmail function signature should look like 您的changeEmail函数签名应如下所示

function changeEmail(userObject, emailString) {

}

Replace the user's current email address (assigned to the email property) with the newEmail string 用newEmail字符串替换用户当前的电子邮件地址(分配给email属性)

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 ? 什么是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: 如果您想通过一些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 . 创建一个名为changeEmail的函数,该函数接受一个用户对象和一个newEmail字符串 Replace the user's current email address (assigned to the email property) with the newEmail string, then return the updated user object. 用newEmail字符串替换用户的当前电子邮件地址(分配给email属性),然后返回更新的用户对象。

When looking at your code, the very first thing to notice is that your function is only accepting one param, not two. 在查看代码时,首先要注意的是您的function仅接受一个参数,而不接受两个。 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 . 另一个注意事项是,您通常希望以更具描述性的方式命名params ,因此在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 现在,新用户位于updatedUser

A user object can be something simple like this, 用户对象可以像这样简单,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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