[英]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.
You could use Object.assign
with a short hand property . 您可以将
Object.assign
与短手属性一起使用 。
const myObj = {} const foo = 'value' Object.assign(myObj, { foo }); console.log(myObj);
If you switch around your statements, you could do it: 如果你转换你的陈述,你可以这样做:
const foo = 'value';
const myObject = { foo };
or if you change from const
to var
so you can assign a new object to your myObj
: 或者如果你从
const
更改为var
那么你可以为myObj
分配一个新对象:
var myObj = {};
const foo = 'value';
myObj = { foo }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.