返回

如何在嵌套的对象数组中获取对象

发布时间:2022-07-25 23:32:52 316
# typescript

在Angular-14应用程序中,我有一个JSON响应:

{
  "data": {
    "pageItems": [
      {
        "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "merchantId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "userId": "string",
        "merchant": {
          "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
          "merchantName": "string",
          "accountNumber": "string",
          "userLists": [
            {
              "id": "string",
              "firstName": "string",
              "lastName": "string",
              "email": "string"
            }
          ]
        }
      }
    ],
  },
  "successful": true,
  "message": "string",
  "statusCode": 0
}

然后,我将it模型界面表示为如下所示:

merchant-user-list.model:

export interface IUserList {
  id: string;
  firstName: string;
  lastName: string;
  email: string;
}

export interface IMerchant {
  id: string;
  merchantName: string;
  accountNumber: string;
}

export interface IPageItem {
  id: string;
  merchantId: string;
  userId: string;
  merchant: IMerchant;
}

export interface IData {
  pageItems: IPageItem[];
}

export interface IMerchantUserList {
  data: IData;
  successful: boolean;
  message: string;
  statusCode: number;
}

但是,我想使用firstName、lastName和email搜索DB表。所以我在typescript组件中做了这个。

merchant-users-list.component:

import { MerchantUserService } from 'src/app/services/merchant-user.service';
import { IData, IPageItem, IMerchantUserList } from 'src/app/models/merchant/merchant-user-list.model';

export class MerchantUsersComponent implements OnInit {
  allMerchantUserList: any[] = [];
  dataBk: IPageItem[] = this.allMerchantUserList;

  constructor(
    private merchantService: MerchantUserService
  ) {
  }

  onMerchantUserSearch() {
    this.allMerchantUserList = this.dataBk.filter(
      (row) =>
        row.merchant.userLists[0].firstName
          ?.toLowerCase()
          .includes(this.selectedName?.toLowerCase()) && row.merchant.userLists[0].lastName
          ?.toLowerCase()
          .includes(this.selectedName?.toLowerCase()) && row.merchant.userLists[0].email
          ?.toLowerCase()
          .includes(this.selectedName?.toLowerCase())
    );
  }
}

我想在文本框中使用firstName、lastName和email来过滤(搜索)嵌套数组中的所有对象。

我上面的只会得到长度为0的对象。当我做row.merchant.userLists时。firstName,我得到一个错误,firstName,lastName和email在用户列表中不可用。

如何重写onMerchantUserSearch()来实现这一点?

谢谢

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像