I'm using node-red and I have the following incoming msg.payload:
[ "=00ECY20WA200_RECV_P1SEL", "true", "=00ECY20WA200_RECV_P2SEL", "true", "=00ECY20WA300_RECV_P2SEL", "true", "=00ECY20WA300_RECV_P1SEL", "true", "=00ECY20WA202_RECV_P1SEL", "true", "=00ECY20WA202_RECV_P2SEL", "false", "=00ECY20WA303_RECV_P2SEL", "true", "=00ECY20WA303_RECV_P1SEL", "true", "=00ECY20WA204_RECV_P1SEL", "false", "=00ECY20WA204_RECV_P2SEL", "true", "=00ECY20WA305_RECV_P2SEL", "false", "=00ECY20WA305_RECV_P1SEL", "false", "=00ECY20WA205_RECV_P1SEL", "false", "=00ECY20WA205_RECV_P2SEL", "false", "=00ECY20WA306_RECV_P1SEL", "true", "=00ECY20WA306_RECV_P2SEL", "true", "=00ECY20WA206_RECV_P1SEL", "false", "=00ECY20WA206_RECV_P2SEL", "true", "=00ECY20WA307_RECV_P1SEL", "true", "=00ECY20WA307_RECV_P2SEL", "true", "=00ECY20WA207_RECV_P1SEL", "false", "=00ECY20WA207_RECV_P2SEL", "false", "=00ECY20WA308_RECV_P1SEL", "false", "=00ECY20WA308_RECV_P2SEL", "true", "=00ECY20WA208_RECV_P1SEL", "false" ]
I'm trying to parse all the items which are "true" and concatenate them in an array (recievingAlarms), the parsed item being the one located just before the Boolean operator. I'm trying to do this with a for loop and I'm pretty sure I've created an infinite loop, I'm not sure how to correct it. Here is what I have:
var recievingAlarms = [];
for (i = 1; i < msg.payload.length; i+2)
if(msg.payload[i] === true) {
recievingAlarms.concat(msg.payload[i-1]);
}
msg.payload = recievingAlarms;
return msg;
This is your solution
var recievingAlarms = [];
for (i = 1; i < msg.payload.length; i=i+2)
if(msg.payload[i] == "true") {
recievingAlarms=recievingAlarms.concat(msg.payload[i-1]);
}
msg.payload = recievingAlarms;
return msg;