抓取特定的对象属性,而不是直接引用

我试图在我的checkForUrgentEvents方法中获取特定的对象属性并返回它。不用引用我当前的方式,而是可以找到特定的属性?我尝试了Object.hasownproperty,但是由于它嵌套得很深,所以无法正常工作。


  private checkForUrgentEvents(urgentEventsData: any, query:CurrentContentParams) {

  return urgentEventsData.data.Data[

      query.type + caasConfig.urgentContentIdSuffix

    ].content[query.id];


  }



// urgentEventsData:


{ status: 200,

  statusText: 'OK',

  headers:

   { connection: 'close',

     'transfer-encoding': 'chunked' },

  config:

   { adapter: [Function: httpAdapter],

     transformRequest: { '0': [Function: transformRequest] },

     transformResponse: { '0': [Function: transformResponse] },

     timeout: 0,

     xsrfCookieName: 'XSRF-TOKEN',

     method: 'get',

  data:

   { LatestModified: 1555080079154,

     Results: 1,

     Error: '',

     Data: { faqsurgentcontent: [Object] } } }


//query:

{ id: 'MAIN', type: 'faqs' }


小唯快跑啊
浏览 146回答 2
2回答

芜湖不芜

我遇到了类似的问题,请遵循以下步骤:&nbsp;parseContentResponse(response: any, scope: string, type: string) {&nbsp; &nbsp; if (response) {&nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; this.getContentObject(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['data', 'Data', `${scope}`, 'content', `${type}`]&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; return response;&nbsp; }//then you could look for the key&nbsp;private getContentObject(response: any, contentPathArray: Array<string>) {&nbsp; &nbsp; return contentPathArray.reduce(&nbsp; &nbsp; &nbsp; (obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined),&nbsp; &nbsp; &nbsp; response&nbsp; &nbsp; );&nbsp; }

江户川乱折腾

您可以尝试以下方法:function findById(o, id) {&nbsp; &nbsp; //Early return&nbsp; &nbsp; if( o.id === id ){&nbsp; &nbsp; return o;&nbsp; &nbsp; }&nbsp; &nbsp; var result, p;&nbsp;&nbsp; &nbsp; for (p in o) {&nbsp; &nbsp; &nbsp; &nbsp; if( o.hasOwnProperty(p) && typeof o[p] === 'object' ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = findById(o[p], id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(result){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript