How do I use expressions to compare values of properties (objects)

Be careful when using comparisons i.e. == and != for instance within an "if" statement in conjunction with node.property syntax.

For a comparison to work correctly, OrgVue must be able to identify the object type.  In the following example this "identification" will not be effective:

The objective of the code below is to indicate when a node has the same business area as their parent node.

if(node.businessarea == node.p.businessarea) {
"same"
}
else {
"different"
}

which will only ever evaluate to "different" regardless of the business area under analysis.

The problem here is that both node.businessarea and node.p.businessarea are object references without a declared type, and OrgVue is trying to evaluate the expression without first having had the object types set. Because the object types are not set, the evaluation fails as OrgVue cannot consider as equal two possibly different object types.

When comparing object references, the required approach is to add the keyword .value on the right hand side of the expression to ensure the type and value of the object reference is evaluated correctly i.e.

if(node.businessarea == node.p.businessarea.value) {
"same"
}
else {
"different"
}

which will return "same" provided that the type and value of the object references node.businessarea and node.p.businessarea are the identical e.g. Finance.

 

Another way to avoid this problem is to set the object type explicitly on the right hand side of the evaluation:

node.entrygrade != 5 node.companyname == "Concentra"

i.e. the first expression evaluates node.entrygrade as a numeric object type and checks if the value is not 5. The second expression evaluates node.companyname as string type and checks if the value is equal to "Concentra".

An alternative, simplified, way to explain the above is that OrgVue's extended functionality means

node.property

is typically an object with additional options available after the dot e.g.

node.property.sum

For comparisons to work, it is sometimes required to explicitly turn objects into primitives i.e.

  • a number
  • text

not an object.  If object based comparison is ineffective, force the comparison via primitives by adding .value to the property being compared, on the right hand side of the equation i.e.

if (node.property != node.propertyB.value) {}

 

 

 

 

 

 

 

 

 

 

Have more questions? Submit a request

Comments