admin管理员组文章数量:1323707
What's the cleanest way to destructure the following object?
const e = {
target: {
userid: {
value: 'abc'
},
password: {
value: 'xyz'
}
}
}
The object is how an HTML form returns data and I'm trying to retrieve values using ONLY destructuring. My attempt was:
const {target: {userid: {value}, password: {value}}} = e;
But it chokes on the two value
s having the same property name. Any ES6 alternative?
What's the cleanest way to destructure the following object?
const e = {
target: {
userid: {
value: 'abc'
},
password: {
value: 'xyz'
}
}
}
The object is how an HTML form returns data and I'm trying to retrieve values using ONLY destructuring. My attempt was:
const {target: {userid: {value}, password: {value}}} = e;
But it chokes on the two value
s having the same property name. Any ES6 alternative?
-
1
Side note:
e: {...}
should bee={...}
– FZs Commented Aug 20, 2019 at 6:50 - 1 But you must be sure that the keys are going to be available. Otherwise, you have to default the destructured object (parent). – Ioan Commented Aug 20, 2019 at 6:53
1 Answer
Reset to default 12You can destructure the value properties into distinctly named variables by placing the names after a :
, e.g.
const {target: {userid: {value: myUserId}, password: {value: myPassword}}} = e;
myUserId
will now have the value 'abc' and myPassword
'xyz'
本文标签: javascriptHow to destructure an object with multiple subkeys of the same nameStack Overflow
版权声明:本文标题:javascript - How to destructure an object with multiple sub-keys of the same name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742128970a2422070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论