我正在使用React / Redux,并且有一些嵌套的reducer,我也想对其进行分解和分配变量。这是一个例子
car = {
type: "mazda",
year: 2011,
tireCondition: {
frontLeft: {
innerEdgeWear: .22,
outerEdgeWear: .42,
},
frontRight: {
innerEdgeWear: .29,
outerEdgeWear: .42,
},
rearLeft: {
innerEdgeWear: .23,
outerEdgeWear: .32,
},
rearRight: {
innerEdgeWear: .31,
outerEdgeWear: .42,
},
}
我想为我的计算使用4个变量,分别称为frontLeftInnerEdgeWear,frontRightInnerEdgeWear,rearLeftInnerEdgeWear,rearRightInnerEdgeWear。我知道我可以使用嵌套对象分解来访问一个值,并给它一个合适的名称,如下所示:
const { tireCondition : { frontLeft : {innerEdgeWear: frontLeftInnerEdgeWear }}} = car
然后如果我console.log frontLeftInnerEdgeWear我得到0.22。
我是否可以使用对象分解来获取和命名所有变量,即使它们是同级变量?
我以为这也许行得通,但那不是
const { tireCondition : {
frontLeft : {innerEdgeWear: frontLeftInnerEdgeWear }},
frontRight : {innerEdgeWear: frontRightInnerEdgeWear }} = car