-2

We all know about destructuring objects in ES6+

const myObject = {name: 'John', age: 30, eyeColor: 'brown'}
const {name, age, eyeColor} = myObject

... is there a "cleaner" way to do the following?

const name = 'John'
const age = 30
const eyeColor = 'brown'

myObject.name = name
myObject.age = age
myObject.eyeColor = eyeColor

Not a great example but in some cases I have to do this with 18 or so variables and it's ugly. Is there an elegant method or utility to do this?

anthony galligani
  • 386
  • 1
  • 3
  • 16

1 Answers1

0

Yea, you don't have to explicitly write out the value if the key and variable name are the same

const myObject = {name, age, eyeColor}
Andrew
  • 7,201
  • 5
  • 25
  • 34