2017-05-20 105 views
0

我需要3個變量cxcxweatherIcon從對象this.props.payload目前我使用這個代碼解構賦值爲嵌套屬性

const { cx, cy } = this.props 
const {weatherIcon} = this.props.payload 

它的工作原理,但我想知道是否有可能寫在一行。

回答

2

試試這個。

const { cx, cy, payload: { weatherIcon } } = this.props; 

const props = { cx: 1, cy: 2, payload: { weatherIcon: 3 }}; 
 
const { cx, cy, payload: { weatherIcon } } = props; 
 
console.log(cx, cy, weatherIcon);