给出以下示例。是否有字段分配的简写
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.
最佳答案
您可以使用 Object.assign
与 short hand property .
const myObj = {}
const foo = 'value'
Object.assign(myObj, { foo });
console.log(myObj);
关于javascript - 与对象解构相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47692708/