Hello,
I'm a totally newby with node-red (and javaScript). I'm receiving an array and would like to look for an entry with two specific values. It these values match, set a variable to 1 (to grant access and open the door) and terminate the loop. I have tried with the following function node, but it complains because there is an 'Error in JavaScript code'
let access=0;
for (i = 0; i < msg.payload.length; i=i+1)
if(msg.payload[i].clients[0].idColegiat = "3554" && payload[i].idSala = "3") {
access = 1;
i = msg.payload.length;
}
msg.payload = authorize;
return msg;
Any hint or advise would be really appreciated. Thanks!!
Hi and Welcome,
Something like?
let access = 0;
for (let i = 0; i < msg.payload.length; i++) {
if (msg.payload[i].clients[0].idColegiat == "3554" && payload[i].idSala == "3") {
access = 1;
break;
}
}
msg.payload = access;
return msg;
2 Likes
omrid
3 November 2025 21:13
3
You can also use the JS built in array method find(), which searches for the first entry which meets a specified condition. For example:
const found = userArray.find(user => user.username === "alice");
if (found) {
...
}
2 Likes
jbudd
3 November 2025 21:14
4
Welcome to the forum @alebaca
You need to declare the variable i
for (let i=0; ...
You need to put braces round the for code block
for (let i = 0; i < msg.payload.length; i=i+1) {
if(msg.payload[i].clients[0].idColegiat = "3554" && payload[i].idSala = "3") {
access = 1;
break;
}
}
And explicitly breaking from the loop is better form than setting a high value for i
i++ or i+=1 are a bit nicer than i = i + 1
1 Like
Colin
4 November 2025 07:15
5
Also need == for testing equality.
2 Likes
Thanks! It worked (after adding a msg. in front of payload[i]idSala I had also forgotten). Thanks for all your suggestions!
Thanks! I will take a look to that function also (but, by the moment, it's too much for me; the old-style 'if' is working.
jbudd:
i=i+1
It is the first time i see this in a for loop. I didnt even think it was possible. Im so used to using i++. What i noob I am
Another alternative is to use array.some
msg.payload = msg.payload.some(item => item.clients[0].idColegiat === "3554" && item.idSala === "3") ? 1 : 0;
return msg
1 Like
jbudd
4 November 2025 22:25
11
I think it's fair to say that @alebaca managed to pack quite a few beginners' mistakes into a few lines of javascript, none of us managed to spot all of them.
Still, everyone is a beginner sometime, I hope Alejandro is not too discouraged by this experience
1 Like
thanks all for your suggestions and comments!
1 Like
I would like to post the whole node before closing the post. Thanks all for your contributions!
let access = 0;
for (let i = 0; i < msg.payload.length; i++) {
if (msg.payload[i].clients[0].idColegiat == msg.idnumber && msg.payload[i].idSala == "1") {
access = 1;
break;
}
}
msg.access = access;
return msg;
PS: msg.idnumber is provided by a previous node (decoded from a QR code by an ESP32 cam) and the array is provided by an external DB (API query)
jbudd
13 November 2025 20:10
14
Just to point out that this test will succeed if msg.payload[i].idSala is equal to the number 1, the string "1" or boolean true.
That is, (I think) these tests are all equivalent:
if (msg.payload[i].idSala == "1") ...
if (msg.payload[i].idSala == 1) ...
if (msg.payload[i].idSala == true) ...
If you want to test if the value is the string "1" you ought to use javascript's === operator:
if (msg.payload[i].idSala === "1")
1 Like
system
Closed
27 November 2025 20:10
15
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.