给予
this.state = {email: "something", firstName: "something" }
const name = "email"
const value = this.state[name]
我想制作一个这样的新物体
{email: "something"}
基本上没有其他东西
我尝试过
const new = {name: value}
这使其成为{name:“ something”}我想要的{email:“ something”}
但是键没有附加到变量上,我希望我不知道电子邮件
答案 0 :(得分:1)
您的代码有两个问题。 new
是保留字,因此您不能创建名为new
的变量。其次,您要执行的操作是这样的:
const name = 'email';
const myObj = {
[name]: 'something'
};
答案 1 :(得分:0)
在将键作为变量提供时,请使用[]
var name = "email"
var value = "something"
{[name]: value}
=> {email: "something"}