老师,请教个关于解构赋值的问题
来源:2-36 Desctructuring

目訫
2020-04-28
let options = {
size: {
width: {
size:{
width:100
}
},
height: 200
},
items: ['Cake', 'Donut'],
extra: true
}
上面这个是课堂的例子,假如现在既想取第一个width的数据,又想取第二个width的数据,应该怎么做
如果这么做
let { size: { width: { size: { width } }, height }, items: [item1, item2], extra } = options
那么取到的将会是最后一个width的值,是不是证明解构除了最后一个变量,其余的变量都是代表结构
这么做可以做到需求
let { size: { width } } = options
let { size: { width: { size: { width: width11 } } } } = options
请问老师还有什么好办法
写回答
1回答
-
没什么好的办法,因为按照解构赋值,必须要要结构做映射(key)或则 value 部分改变名称,既想在value改变名称又想再value部分获取子结构那就冲突了
00
相似问题