1

I have an array of users and each user has couple of roles. The example of the array is shown below.

[
    {
        id: 1,
        username: 'john',
        roles: [
            {id: 500, name: 'Admin'},
            {id: 501, name: 'Owner'}
        ]
    },
    {
        id: 2,
        username: 'joe',
        roles: [
            {id: 500, name: 'Admin'},
        ]
    },
    {
        id: 3,
        username: 'june',
        roles: [
            {id: 502, name: 'User'},
        ]
    }
]

I'm trying to get all the users who have an Admin role by using jmesPath. I've tried [?roles[].name=='Admin'] but this is giving a null value. Is is possible to do this with jmesPath, and if yes can you give me an expression for this example?

4

2 回答 2

3

我玩了这个例子和你的数据

http://jmespath.org/examples.html#filtering-and-selecting-nested-data

我使它与这个表达式一起工作

[?roles[?name=='Admin']]
于 2017-08-16T00:05:32.783 回答
0

您可以使用纯 JavaScript 获得相同的结果:

假设您的数组存储在 var users = [...] 上。

users.filter((i)=> i.roles.some((s) => s.name === "Admin"))

您可以使用 jsBin 查看此示例

于 2017-08-15T23:48:21.457 回答