过滤json对象数组

帕特尔·皮伊

下面的数据是getUserRoles函数的结果。我们如何过滤它以检查“associatedAccount”:值,然后检查userAccountDto:accountName中是否存在associatedAccount值。如果存在或值相同,则从 userAccountDto 获取 userRoleDTO: roleName 值并返回结果作为 getUserRoles 的结果。

例如“associatedAccount”:“testcompany”存在于userAccountDto”:即“accountName”:“testcompany”,然后是“userRoleDto”:“roleName”:“Admin”。将rolename输出作为数组返回,因为

getUserRoles 的最终结果是 res.data 应该基于逻辑bove是存储在数组中的“Admin”。

感谢您的任何想法或帮助。

#数据

{
  "id": 2,
  "emailAddress": "test",
  "firstName": "test",
  "lastName": "test",
  "mobileNumber": "test",
  "associatedAccount": "testcompany",
  "status": "Active",
  "lastLogIn": null,
  "invitedById": null,
  "invitedByDate": null,
  "identityId": "2",
  "userAccountDto": [
      {
          "id": 2,
          "accountId": 4,
          "accountName": "testcompany",
          "displayName": "testcompany",
          "userRoleDto": {
              "id": 2,
              "roleName": "Admin"
          },
          "accountDto": {
              "accountId": 4,
              "accountName": "test",
              "displayName": "test",
              "isActive": true,
              "contactFirstName": null,
              "contactLastName": null,
              "contactPhone": null,
              "contactEmailAddress": null,
              "accountRoleDto": [
                  {
                      "id": 1,
                      "accountId": 4,
                      "roleName": "Admin"
                  },
                  {
                      "id": 2,
                      "accountId": 4,
                      "roleName": "Broker"
                  },
                  {
                      "id": 5,
                      "accountId": 4,
                      "roleName": "Transaction Manager"
                  },
                  {
                      "id": 6,
                      "accountId": 4,
                      "roleName": "Transaction Super User"
                  },
                  {
                      "id": 7,
                      "accountId": 4,
                      "roleName": "Unlicensed User"
                  }
              ]
          }
      },
      {
          "id": 3,
          "accountId": 2,
          "accountName": "testcompany2",
          "displayName": "testcompany2",
          "userRoleDto": {
              "id": 3,
              "roleName": "Admin"
          },
          "accountDto": {
              "accountId": 2,
              "accountName": "testing",
              "displayName": "Bank of test",
              "isActive": true,
              "contactFirstName": null,
              "contactLastName": null,
              "contactPhone": null,
              "contactEmailAddress": null,
              "accountRoleDto": [
                  {
                      "id": 3,
                      "accountId": 2,
                      "roleName": "Admin"
                  },
                  {
                      "id": 4,
                      "accountId": 2,
                      "roleName": "User"
                  }
              ]
          }
      }
  ]
}

#获取用户角色的函数

public getUserRoles(): Promise<string[]> {
    return new Promise((resolve, reject) => {
      this.http.get(`${this.baseUrl}getUserRoles`)
        .pipe(catchError((error: any, caught: any) => {
          reject(error);
          return caught;
        }),
          map((res: any) => res.data ))
        .subscribe((role: string[]) => {
          resolve(role);
        });
        
    });
  }

#snippet 函数调用 getUserRoles

private checkPermission(allowedUserRoles: Roles[]): Promise<boolean> {
    return this.authService.getSession().then((session: boolean) => {
      if (session) {
        if (!allowedUserRoles) {
          return true;   // if no user roles has been set, all user are allowed to access the route
        } else {
          return this.authService.getUserRoles().then((userRoles: string[]) => {
jpf

此代码将过滤匹配的帐户,然后映射您想要的值:

data.userAccountDto
               .filter(account => account.accountName === data.associatedAccount)
               .map(account => account.userRoleDto.roleName);

所以你可以像这样把它放在你的函数中:

public getUserRoles(): Promise<string[]> {
    return new Promise((resolve, reject) => {
      this.http.get(`${this.baseUrl}getUserRoles`)
        .pipe(catchError((error: any, caught: any) => {
          reject(error);
          return caught;
        }),
          map((res: any) => res.data && res.data.userAccountDto
                                         .filter(account => account.accountName === res.data.associatedAccount)
                                         .map(account => account.userRoleDto.roleName); 
        )).subscribe((role: string[]) => {
          resolve(role);
        });
        
    });
  }

所以 filter() 运算符将对数组中的每个项目进行比较并返回一个真实项目的数组。然后 map 操作符将映射每个项目并返回一个映射项目数组......在这种情况下,您想要的单个字段。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章