[英]Destructuring Object with Enum in Javascript
I try to find a way to destructuring keys of the object in ES6 with keys of another object (Eum) and create variables. 我试图找到一种方法来用另一个对象(Eum)的键来破坏ES6中对象的键并创建变量。 for example my enum is
例如我的枚举是
const KEYS = {
name:'name',
family:'age'
}
and my object is : 我的对象是:
const myObject = {
name:'John',
family:'Doe'
}
and what I want is destructuring myObject
with keys of KEYS
I know the below code is wrong but something like this : 我想要的是用
KEYS
来破坏myObject
,我知道以下代码是错误的,但类似这样:
const {KEYS.name} = myObject
so it creates a variable with name name
that is the value of KYES.name
but as I say it raised a syntax error . 因此,它创建了一个名称为
name
的变量,该变量的名称为KYES.name
的值,但是正如我所说的,它引发了语法错误。 does anyone has an idea for a destructuring object with an enum. 没有人有一个构想用枚举来破坏对象。
You could use a computed property names and an object property assignment pattern [YDKJS: ES6 & Beyond] for it. 您可以使用计算的属性名称和对象属性分配模式[YDKJS:ES6&Beyond] 。
name
is a reserved property of window
and may lead to unexpected results by changing it. name
是window
的保留属性,通过更改它可能导致意外结果。
const KEYS = { name: 'name', family: 'age' }, myObject = { name:'John', family:'Doe' }; ({ [KEYS.name]: window[KEYS.name] } = myObject); console.log(name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.