从对象打字稿中获取项目数组

Get an array of items from an object- typescript

我有一个具有以下结构的对象:

  Order: [
    {
      id: '001',
      type: '',
      status: '',
      users: [
        {
          OrderId:'001', 
          userId: 'String',
          user: {
            email: 'string',
            givenName: 'Name',
            lastName: 'LastName',
            phone: 'string',
          },
        },
      ],
  
    },

订单类型为 Order[],用户类型为 UserData[]

  type Order
  @model
  @key(fields: ["organizationId", "id"])
  @auth(
    rules: []
  ) {
  id: ID!
  type: OrderType
  status: OrderStatus

  userConnections: [UserOrderConnection] @connection(keyName: "byOrder", fields: ["id"])

  organizationId: ID!
}

type UserOrderConnection
  @model
  @key(fields: ["organizationId", "id"])
  @key(name: "Order", fields: ["OrderId"], queryField: "userOrderByOrder")
  @auth(
    rules: []
  ) {
  id: ID!
  accepted: Boolean
  OrderId: ID!
  Order: Order @connection(fields: ["organizationId", "id"])
  userId: ID!
  user: UserData
}

每当我尝试获取每个订单的用户列表时:

let users = this.Order.users

它说:用户不存在于 Order[] 类型上,任何人都可以为我解释为什么。

Order是一个对象数组,需要像Order[0]一样获取第一个元素,然后访问.users

let users = this.Order[0].users

好好复习一下可能会有帮助; How can I access and process nested objects, arrays or JSON?

Order 是一个包含对象的数组。要访问 users,您需要访问索引 [0] 处的项目,如果它有 属性 ?.users.

this.Order[0]?.users