如何解构嵌套对象

5
这是我从POST请求中获取的response.data。我只想从中获取ObjectID。
 {
     d: {
        results: {
          __metadata: [Object],
          ObjectID: '00163E6CDDFC1EEA96C57123A5C6DDE5',
          InformationLifeCycleStatusCode: 'AC',
          ID: '9000001424'
        }
      }
    }

这是我目前的结果,但我需要以某种方式进入结果对象。
        const{"ObjectID: objectID} = res.data;

你的代码中没有 resdata - palaѕн
这是 MDN 上的一个示例:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Unpacking_fields_from_objects_passed_as_function_parameter - Andreas
1
如果您只想获取objectId,则使用以下代码:const objectId = res.data.d.results.ObjectID - raq
1个回答

5

You need to give complete path when you have to destrcuture a nested property.

const obj =  {
     d: {
        results: {
          __metadata: [Object],
          ObjectID: '00163E6CDDFC1EEA96C57123A5C6DDE5',
          InformationLifeCycleStatusCode: 'AC',
          ID: '9000001424'
        }
      }
    }

const {d : {results : {ObjectID}}} = obj;

console.log(ObjectID)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接