简体   繁体   English

过滤json对象数组

[英]filter json array of objects

The data below is the result of getUserRoles function.How do we filter it to check the "associatedAccount": value and then check if associatedAccount value exists in userAccountDto : accountName.下面的数据是getUserRoles函数的结果。我们如何过滤它以检查“associatedAccount”:值,然后检查userAccountDto:accountName中是否存在associatedAccount值。 If exist or value is the same then get userRoleDTO: roleName value from userAccountDto and return result as result of getUserRoles .如果存在或值相同,则从 userAccountDto 获取 userRoleDTO: roleName 值并返回结果作为 getUserRoles 的结果。

For example "associatedAccount": "testcompany" exists in userAccountDto": which is "accountName": "testcompany", and then the "userRoleDto": "roleName": "Admin". Return the rolename output as array because例如“associatedAccount”:“testcompany”存在于userAccountDto”:即“accountName”:“testcompany”,然后是“userRoleDto”:“roleName”:“Admin”。将rolename输出作为数组返回,因为

the final result of getUserRoles which is res.data should be for example based on the logic bove is "Admin" stored in an array. getUserRoles 的最终结果是 res.data 应该基于逻辑bove是存储在数组中的“Admin”。

Thanks for any idea or help.感谢您的任何想法或帮助。

#Data #数据

{
  "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"
                  }
              ]
          }
      }
  ]
}

#function to get user roles #获取用户角色的函数

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 function that calls getUserRoles #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[]) => {

This code will filter the accounts for the match and then map the value you want:此代码将过滤匹配的帐户,然后映射您想要的值:

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

So you could put this in your function like so:所以你可以像这样把它放在你的函数中:

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);
        });
        
    });
  }

So the filter() operator will make the comparison on each item in an array and return an array of truthy items.所以 filter() 运算符将对数组中的每个项目进行比较并返回一个真实项目的数组。 Then the map operator will map each item and return an array of mapped items... in this case that single field you wanted.然后 map 操作符将映射每个项目并返回一个映射项目数组......在这种情况下,您想要的单个字段。

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((response)=> {
            var result  = string[];
            response.foreach(item=> {
                if(item.associatedAccount) {
                    item.userAccountDto.foreach(deepItem=> {
                        if(item.associatedAccount == deepItem.accountName) {

                            result.push(deepItem.userRoleDto.roleName);
                        }
                    })
                }
            })

return  {
    data : result
}
        })
          map((res: any) => res.data ))
        .subscribe((role: string[]) => {
          resolve(role);
        });
        
    });
  }

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

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