Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opposite of object destructuring

Given the following example. Is there a shorthand for the field asignment

let myObj = {someField: 'someValue'}
let foo = 'value'

myObj.foo = foo // I would like to do the opposite of object destructuring: const {foo} = myObj. I don't want to repeat foo twice.
like image 839
Lev Avatar asked Dec 23 '22 11:12

Lev


1 Answers

You could use Object.assign with a short hand property.

const myObj = {}
const foo = 'value'

Object.assign(myObj, { foo });

console.log(myObj);
like image 56
Nina Scholz Avatar answered Jan 08 '23 17:01

Nina Scholz