which command return if label doesnt exist in item?

which command return if label doesnt exist in item?

gabriel_illescas_cavazos
Not applicable
820 Views
6 Replies
Message 1 of 7

which command return if label doesnt exist in item?

gabriel_illescas_cavazos
Not applicable
0 Likes
Accepted solutions (1)
821 Views
6 Replies
Replies (6)
Message 2 of 7

mischa_spelt
Advisor
Advisor
Accepted solution

If you want to know whether the label exists and has a "truthy" value, you can use the question mark:

if(current.maynotexist?) {
 // current.maynotexist exists and is non-zero, or points at a valid object
}

If you just want to know if it exists, you can do

treenode maynotexist = current.labels["maynotexist"];
if( maynotexist ) {
  // the node exists, get its value with maynotexist.value
}

However, you don't need the latter construction very often, for example, if you want to create the label if it does not exist, you can

current.labels.assert( "maynotexist" )

and if you assign a label with

current.maynotexist = 1;

it will be automatically created.

Message 3 of 7

gabriel_illescas_cavazos
Not applicable

I want to set this condition for example if any label exist do nothing but if the label doesnt exist do something how can I do it?

0 Likes
Message 4 of 7

Jacob_Gillespie
Autodesk
Autodesk
@Gabriel illescas Cavazos
if(!obj.MyLabel? && obj.MyLabel? != 0) {
    // Label not set
    // Do Something
}
0 Likes
Message 5 of 7

gabriel_illescas_cavazos
Not applicable

what does it mean "!" when you star the if?

0 Likes
Message 6 of 7

A "!" is a logical not.

This will return true only for strings that start with h:

text.startsWith("h")

This will return true for any string that starts with any letter other than h:

!text.startsWith("h")


Matthew Gillespie
FlexSim Software Developer

0 Likes
Message 7 of 7

vedant_g1
Not applicable

I tried the ? method but it doesn't work for me

12508-capture.png

12509-capture-2.png

0 Likes