I am trying to compare a number of message properties in a function node and would like to do this with variables rather than repeating each object path. The trouble I am having is that I can't work out how to build the variables into the object path.
What I have so far is:
//variables
var current = "v_hallightgrp";
var target = "c_lightnight";
//compare brightness
if (msg.payload.+current+.brightness !== msg.payload.+target+.brightness) {
This is throwing an error. I know putting it in quotes isn't the answer as that converts the object paths to strings. Thanks in advance for your help!
Edit:
Strings defined correctly for variables (thanks Johannes)
Where did you define v_hallightgrp and c_lightnight or did you mean them to be strings? Because the way you wrote it above without " before and after is trying to reference a variable of the name v_hallightgrp that in itself would have to be defined somewhere.
That should be if (msg.payload[current].brightness !== msg.payload[target].brightness) {
The dot syntax is a shorthand for the square bracket notation, so the above line is actually a shorthand way of writing if (msg["payload"][current]["brightness"] !== ....