ES6深度嵌套对象解构

ES6 deep nested object destructuring

本文关键字:对象 嵌套 深度 ES6      更新时间:2023-09-26

我有一个名为this.props的对象,它包含

{
 actions: Object,
 dirty: false,
 form: "Statement",
 autofill: function(),
 **statement: Object**
}

statement包含

{
 firstName: "John"
 lastName: "Peter"
 isConfirmed: true
}

我想使用es6解构在同一行中提取statement对象和isConfirmed属性

我试过了

const { statement: isConfirmed, isAdmin } = this.props

当我执行let a = isConfirmed, b = statement

时,我得到了一个错误

我想在同一行中提取语句对象和isConfirmed属性

const { statement: { isConfirmed }, statement } = this.props;

这样你就得到了isConfirmed和整个statement对象。

引用:

  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment Nested_object_and_array_destructuring