简体   繁体   English

如何访问 React TypeScript 中的嵌套数据接口

[英]How to access nested data interface in React TypeScript

In React TypeScript, what is the appropriate way to access data in a nested interface like the one below?在 React TypeScript 中,在如下所示的嵌套接口中访问数据的适当方法是什么?

export interface School {
  prices: Array<{
    state: {
      response_header?: {
        school_type_opportunities?: Array<{ benefit_type_opportunity?: string }>;
        school_alternative_products?: Array<{
          school_alternative_cost_share_incentive?: string;
          school_alternative_description?: string;
          school_alternative_id?: string;
          school_attendance_required?: Array<{
            school_attendance_location?: string;
            school_attendance_blabla?: string;
            school_attendance_blabla2?: string;
          }>;
        }>;

I want to get access to:我想访问:

  • school_type_opportunities
  • school_alternative_description
  • school_attendance_location

TypeScript is merely type-safe JavaScript, ie you'd practise the same methodologies and find eg TypeScript 只是类型安全的 JavaScript,即您将练习相同的方法并找到例如

const schoolTypeOportunities =
  School.prices[x].state.response_header.school_type_opportunities;

As prices is of type Array here (and depending on your application) you'd need to know which index is relevant and change x .由于这里的pricesArray类型(并且取决于您的应用程序),您需要知道哪个索引是相关的并更改x Alternatively, if multiple, you can might find map , filter , or reduce convenient to extract your wanted items.或者,如果有多个,您可能会找到mapfilterreduce来方便地提取您想要的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM