Engineer Passion Engineer Passion - 1 month ago 8
Javascript Question

is there more elegant way to write this condition?

I want a smaller way to write this condition I've tried to simplify it but it doesn't work with me

if(( data.includes(userId) || data.includes(userId.toString()) || data.includes(username) )) console.log(true)


this condition works very good but I want a simplified version of it

Answer Source

You can use the some Array method to test for multiple predicates. Either

if ([userId, userId.toString(), username].some(x => data.includes(x))) …

or

if (data.some(x => [userId, userId.toString(), username].includes(x))) …