0

Given

this.state = {email: "something", firstName: "something" }
const name = "email"
const value = this.state[name]

I want to make a new object like this

{email: "something"}

basically without the other stuff

I tried

const new = {name: value}

This makes it {name: "something"} I want {email: "something"}

but the key doesn't attach to a variable and I want email to not be known to me

2 Answers2

1

You've got two problems with your code. new is a reserved word, so you can't make a variable named new. And second, what you're looking to do is something like this:

const name = 'email';
const myObj = {
  [name]: 'something'
};
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
0

Please use [] while providing key as a variable

var name = "email"
var value = "something"
{[name]: value}
=> {email: "something"}
Gurwinder
  • 509
  • 2
  • 11