How do i use the javascript OR function

Hi, i am trying to make a button appear and disappear whether the tag is true or false, but i have to check more places in the array so i want to use the OR function. But it doesnt work, how do i fix this. This is my code now:
{{ GetTag.data.nodes[0].tags[0] || GetTag.data.nodes[0].tags[1] || GetTag.data.nodes[0].tags[2] || GetTag.data.nodes[0].tags[3] == "aanpassen" ? false : true }}

I hate to be one of those forum dwellers who just posts a link and tells people to RTFM but I think starting here would be a good first step before we try to dig into the specifics:

I see what it does now. But is there a other solution then?

I don't think the solution is necessarily wrong, using an OR is probably the right approach if your question is "are any of the tags false"

But I think what your current logic is doing is going to something like :

is tags[0] "true" or is tags[1] "true" or is tags[2] "true" or is tags3 equal to "aanpassen"

But as per the docs, it'll shortcut where it can and stop checking the expressions in a left-to-right basis as soon as any of them are "true". eg if tags[0] has any value that equates to a "truthy" (check the docs again for more on this) then it won't even get around to checking tags 1 or 2 or 3 and will return, in your logic, a false.

Can you write the logic as a sentence or pseudo code? Might be easier to start there and then construct your javascript.

And how will i do that?

@DionC
When hover over each of the conditionals, which one is true?

Also, let's break this down:

Let's say each of the following are true (this means if any one of the following is NOT true then the second value of the ternary would be the end result (false)
{{GetTag.data.nodes[0].tags[0] || GetTag.data.nodes[0].tags[1] || GetTag.data.nodes[0].tags[2] || GetTag.data.nodes[0].tags[3] === "aanpassen"
?
true : false }}

So, I would do the following
{{ (GetTag.data.nodes[0].tags[0] || GetTag.data.nodes[0].tags[1] || GetTag.data.nodes[0].tags[2] || GetTag.data.nodes[0].tags[3] === "aanpassen") ? true : false }}

this can also be reversed as you originally had it but I always tend to check if all is true first as I think in most cases when hiding a button you're most likely to get a true when loading a form/page/app... but that's just my preference.

What I meant was, can you explain what your desired outcome is in a sentence - ie something like "if all 4 tags values are empty them hide the button" etc

Don't forget that it checks for "truthy" so it will depend on what is in tags[0] etc, so if tags[0] is the word "howdy" then that part of the expression will equate to true.
The way your expression is written you're checking if it has anything that javascript considers a value, not that it has a particular value. Which is why I asked if you can explain what you're trying to do with the logic.

And good shout from ScottR there too, you should be using === not == for comparisons

The ideal output is that i want: If a customer has a certain tag the button shows up. If the customer doesnt have that certain tag the button is hidden.

Ok, so assuming you have data like this you can check using OR like you have been doing but you need to declare each part of the expression fully.

var data = ['hello','aanpassen',false,'nothing',null];

An OR function checking "are any of the values equal to the word 'aanpassen'?" might be:
{{ (data[0] === 'aanpassen' || data[1] === 'aanpassen' || data[2] === 'aanpassen' || data[3] === 'aanpassen') }}
Which would return a boolean TRUE because one of them is matching.
I find it easier to understand if I read it out loud to myself: Is 0 equal to 'aanpassen' OR is 1 equal to 'aanpassen' OR is 2 equal to 'aanpassen' etc etc

If you have multiple tags all checking the same condition, or you want to add more data items later, a long OR becomes harder to read and modify so the lodash _.some function does the same thing:

{{ _.some(data, function(x) { return x==='aanpassen'}) }}

This would return TRUE if any of the data items you pass it are equal to the string 'aanpassen'.

1 Like