Array in if query

Hei all,
I have an array checkerArr = [0,0,0,0,0,0], I change the array content dynamically. At a certain point I want to know, how is the checkerArr like at the moment, is it [0,0,0,0,0,0], or [0,0,0,1,0,0] or [0,1,1,1,0,0] ...

This doesn´t work:

if (checkerArr == [0,0,0,0,0,0] || checkerArr == [0,1,0,0,0,0] || checkerArr == [0,0,1,0,0,0] || checkerArr == [0,0,1,1,0,0] || checkerArr == [0,0,1,1,1,0] || checkerArr == [0,0,1,1,1,1]){
// do things
}

What am I doing wrong?

You could just coerce them to stings.

checkerArr= [0,0,1,1,0,0]

if (checkerArr.toString() == [0,0,0,0,0,0].toString() || checkerArr == [0,1,0,0,0,0].toString() || checkerArr == [0,0,1,0,0,0].toString() || checkerArr == [0,0,1,1,0,0].toString() || checkerArr == [0,0,1,1,1,0].toString() || checkerArr == [0,0,1,1,1,1].toString()){
console.log("match")
} else {

console.log("no match")
}
2 Likes

Hei Mark! Thanks a lot, that works fine! Can you explain, why it has to be done this way?

It simple.

It is basically converting the arrays to a string i.e checkerArr.toString() is "0,0,1,1,0,0" .

And the comparisons are strings. [0,0,1,1,1,1].toString() will become "0,0,1,1,1,1"

You withdrawn code's intension did the same but I was not sure where checkerArr was coming from.

You can get more complex if your needs must.

1 Like

No solution button found.

Check!