Search code examples
javascriptlogical-operators

How to check if a multiple repeated logic conditions are true in JS


I'm trying to make a bot for the popular game Minecraft but I ran into an issue, my bot got stuck in NPCs (I was testing it in a server) and so I decided to make the code ignore the NPCs by seeing their name and then deciding if it should walk up to them or not. Then I met this issue:

var playerFilter = (entity) => entity.type === "player" && entity.username != "fuud" && entity.username != "snowboi" && entity.username != "fugdealer1" && entity.username != "parkourstart" && entity.username != "boatman" && entity.username != "timber + nails" && entity.username != "mrshade"

It's repeated and I'd like to see if there is any way to shorten it into a list or something like that. This variable is used many times in the code.


Solution

  • Use an array and the .includes() method.

    var playerFilter = (entity) => entity.type === "player" && !["fuud", "snowboi", "fugdealer1", "parkourstart", "boatman", "timber + nails", "mrshade"].includes(entity.username)
    
    console.log(playerFilter({type: "player", username: "somethingelse"}))
    console.log(playerFilter({type: "player", username: "mrshade"}))