Array compare not going well

Hello everyone,

i would like to compare an array, when its 1,1 its should give 1 as output and when its 0,0 it should give a 0. Every other combination should give an error message.

i tried this as you can see bellow but it doesnt work ... can someone help pls?

[{"id":"849ba83b943e3dea","type":"debug","z":"2ecd18bc023a4c74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":600,"wires":[]},{"id":"093db9db80f0ac06","type":"function","z":"2ecd18bc023a4c74","name":"","func":"//Waarden toekennen\nStatus= msg.payload\n\n\n//Bewerkingen doen met waarden.\nif (Status === [0,0]) {\n    Status =0;\n    Statusbericht=\"\"\n}\n    \nelse if (Status === [1,1]) {\n    Status =1;\n    Statusbericht=\"\"\n}\n    \nelse {\nStatusbericht =\"Er is een technische fout opgetreden!\";\nStatus= \"\";\n}\n\n//Weergeven in console\nvar msg1 = {payload:Status}\nvar msg2 = {payload:Statusbericht}\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1540,"y":620,"wires":[["849ba83b943e3dea"],["e40a0efd73d202c1"]]},{"id":"e40a0efd73d202c1","type":"debug","z":"2ecd18bc023a4c74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":640,"wires":[]}]

const Status = msg.payload;
if(!Array.isArray(Status) || Status.length !== 2) {
  node.error("ERROR - payload MUST be an array with 2 elements!", msg);
  return;
}

let result = "OK";
if( Status[0] === 0 && Status[1] === 0 ) {
  //both are 0
  msg.payload = 0;
} else if( Status[0] === 1 && Status[1] === 1 ) {
  //both are 1
  msg.payload = 1;
} else {
  result = `Status ${JSON.stringify(Status)} is not valid`;
}

const msg2 = { payload: result }
return [msg, msg2];

:point_up_2: UNTESTED - but should be enough hints to put you on the right track.


EDIT:

The reason Status will NEVER equal [1,1] is because JS compares objects by reference (e.g. compares the memory addresses of the 2 objects for equality). Since Status is 1 object and [0,0] is an entirely different object, their addresses in memory are different. PS, yes, an array is an object (try adding node.warn(typeof [0,0]) to your code);

1 Like

This seems to work too

let Status = msg.payload;
if (JSON.stringify(Status) === JSON.stringify([0,0])){
    msg.payload = "zero";
}
else if(JSON.stringify(Status) === JSON.stringify([1,1])){
    msg.payload = "one";
}
else {
    msg.payload = "neither";
}
return msg;
1 Like

That was very similar to my initial response (un posted) but changed it because if msg.payload was an array with 100000 elements, thats a whole lot of stringifying for no reason :slight_smile:

1 Like

Hmm thats extreme testing! I only considered msg.payload = timestamp instead of array.

Of course you don't need to stringify [0,0], you can substitute "[0,0]" on the RHS of the expression.

@Olivia is there a reason why you don't use const, let or var for your declaration of Status?

1 Like

It is, but I always use the mantra "if it can happen, it will" - I'm a fairly defensive programmer. Not necessarily better, just defensive :wink:

1 Like

because i don't know a lot about node-red :slight_smile:

Thanks a lot Steve !

1 Like

@Steve-Mcl is there a way that msg 2 would only send a text message when there is an error because i work with a notification on the screen and it shouldnt pop up when its okay... i tried moving the return msg2 into the else but that doesnt seem to work either

return null instead of an actual object.

const Status = msg.payload;
if(!Array.isArray(Status) || Status.length !== 2) {
  const errMsg = "ERROR - payload MUST be an array with 2 elements!"
  node.error(errMsg , msg);
  return [null, {payload: errMsg }]; //only send to output pin 2
}

let msg2 = null; //set nnull by default
if( Status[0] === 0 && Status[1] === 0 ) {
  msg.payload = 0;  //both are 0
} else if( Status[0] === 1 && Status[1] === 1 ) {
  msg.payload = 1;  //both are 1
} else {
   msg2 = { payload: `Status ${JSON.stringify(Status)} is not valid` };
}

return [msg, msg2]; //when msg2 is null, nothing will be output on pin2

It is all documented here: Writing Functions : Node-RED

1 Like

Wow, its just that easy :open_mouth:

Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.