【问题标题】:Destructuring assignment for nested properties嵌套属性的解构赋值
【发布时间】:2017-05-20 07:13:43
【问题描述】:

我需要从对象 this.props.payload 中获取 3 个变量 cxcxweatherIcon,目前我正在使用此代码

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

有效,但我想知道是否可以写在一行中。

【问题讨论】:

    标签: javascript ecmascript-6


    【解决方案1】:

    试试这个。

    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);

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 2017-03-18
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多