javascriptarraysdiscorderis

Check if userid matches one in the array


I'm attempting to check if a user's ID is in this array and if they are, also get the "text" from it.

Array:

const staff = [
    {
        user: '245569534218469376',
        text: 'dev'
    },
    {
        user: '294597887919128576',
        text: 'loner'
    }
];

I've tried if (staff.user.includes(msg.member.id)) (Which I didn't think was going to work, and didn't.)


Solution

  • const findUser = (users, id) => users.find(user => user.id === id)
    
    const usersExample = [
      {
        id: '123456765',
        text: 'sdfsdfsdsd'
      },
      {
        id: '654345676',
        text: 'fdgdgdg'
      }
    ]
    //////////////////
    
    
    const user = findUser(usersExample, '123456765')
    
    console.log(user && user.text)