-3

I have an object {key: 'Mother', value: 'Mam'}. I need to get an object like {'Mother': 'Mam'}

fastday
  • 15
  • 3
  • First I tried to do something like const obj = {key: 'Mother', value: 'Mam'}. And then const newOne = {obj.key : obj.value}. But it was a really stupid attempt. Then I tried using Object.defineProperty and failed again( I'm feeling that I'm stuck in very simple place – fastday May 24 '22 at 23:33
  • 1
    You're basically there: {[obj.key]: obj.value}. If you want to use a variable as a key, you need bracket notation. Not dot notation – Andrew May 24 '22 at 23:41

1 Answers1

0

Your question is pretty vague but you could do:

const obj1 = {key: 'Mother', value: 'Mam'}
const obj2 = {[obj1.key]: obj1.value}

// obj2: {Mother: 'Mam'}
Joseph Tams
  • 63
  • 1
  • 5