Engineer Passion Engineer Passion - 6 months ago 24
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))) …


if (data.some(x => [userId, userId.toString(), username].includes(x))) …
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download