3

I have an array that contains some objects:

const array = [
            {
                id: 1,
                username: "username1",
                roles: [
                    {
                        id: 1,
                        name: "ROLE_USER"
                    },
                    {
                        id: 8,
                        name: "ROLE_ADMIN"
                    }
                ]
            },
            {
                id: 2,
                username: "username2",
                roles: [
                    {
                        id: 1,
                        name: "ROLE_USER"
                    },
                    {
                        id: 10,
                        name: "ROLE_TESTER"
                    }
                ]
            },
            {
                id: 3,
                username: "username3",
                roles: [
                    {
                        id: 1,
                        name: "ROLE_USER"
                    },
                    {
                        id: 8,
                        name: "ROLE_ADMIN"
                    }
                ]
            }
        ]

Now, how do I filter these objects by role? I wanted to get users with "ROLE_ADMIN" only. I tried doing something like:

const admins = array.filter(user => user.roles.filter(role => role.name === "ROLE_ADMIN"));

and

const admins = array.filter(user => user.roles.map(role => role.name === "ROLE_ADMIN"));

and

const admins = array.map(user => user.roles.filter(role => role.name === "ROLE_ADMIN"));

but none of these worked. Can someone help me?

1
  • reduce (and some other characters)
    – Geuis
    Jul 18, 2020 at 10:29

5 Answers 5

5

Try to combine filter with some

According to MDN docs:

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

Below snippet could help you

const data = [
  {
    id: 1,
    username: 'username1',
    roles: [
      {
        id: 1,
        name: 'ROLE_USER'
      },
      {
        id: 8,
        name: 'ROLE_ADMIN'
      }
    ]
  },
  {
    id: 2,
    username: 'username2',
    roles: [
      {
        id: 1,
        name: 'ROLE_USER'
      },
      {
        id: 10,
        name: 'ROLE_TESTER'
      }
    ]
  },
  {
    id: 3,
    username: 'username3',
    roles: [
      {
        id: 1,
        name: 'ROLE_USER'
      },
      {
        id: 8,
        name: 'ROLE_ADMIN'
      }
    ]
  }
]

const res = data.filter(user =>
  user.roles.some(role => role.name === 'ROLE_ADMIN')
)

console.log(res)

2

An empty array or object will still be truthy in JavaScript. Add .length > 0 for roles filter (your first option) as below

const admins = array.filter(user => user.roles.filter(role => role.name === "ROLE_ADMIN").length > 0);
0

enter code hereIt will return all the users where all the role types are admin

`const admins = array.filter(user => {
    return user.roles.every(role => role.name === "ROLE_ADMIN");
})`

If you need any one type to be admin inside the roles then :

const admins = array.filter(user => { return user.roles.some(role => role.name === "ROLE_ADMIN"); })

-4

You can use this

const admins = array.filter(user => user.roles.map(role => role.name).includes( "ROLE_ADMIN"));

1
  • Please avoid accusing people. It is not really constructive and can cause fights. On SO in situations like these, we always assume good faith and move on. If you feel like the problem needs to be escalated then please feel free to raise it on Meta Stack Overflow. Oct 25, 2020 at 11:48
-4

Untested but should work.

const admins = array.filter(user => user.roles.map(role => role.name ).includes("ROLE_ADMIN"));
5
  • Why use map to filter? Wrong method
    – Raffobaffo
    Jul 18, 2020 at 10:46
  • care to enlighten me how the answer is wrong? have you tested it with OP's array of users?
    – Kharel
    Jul 18, 2020 at 11:11
  • It works, but thats does not mean is correct. MAP, maps object, filter, filters objects. Easy like this...
    – Raffobaffo
    Jul 18, 2020 at 11:13
  • Yes. You want to make sure? Create a question and ask if that way of doing is correct. So you won't rely just on my opinion.
    – Raffobaffo
    Jul 18, 2020 at 11:17
  • 1
    @Kharel please read the code of conduct Oct 25, 2020 at 11:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.