# Loop within an array

**URL:** <https://discourse.nodered.org/t/loop-within-an-array/99572>\
**Category:** General\
**Tags:** array-of-an-object\
**Created:** [3 November 2025 20:30 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572 "2025-11-03T20:30:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![alebaca](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@alebaca](https://discourse.nodered.org/u/alebaca)\
**Post date:** [3 November 2025 20:30 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/1 "2025-11-03T20:30:28Z")

</div>

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'_

```auto
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!!

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 November 2025 20:42 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/2 "2025-11-03T20:42:19Z")

</div>

Hi and Welcome,

Something like?

```js
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;

```

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [3 November 2025 21:13 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/3 "2025-11-03T21:13:09Z")

</div>

You can also use the JS built in array method find(), which searches for the first entry which meets a specified condition. For example:

```auto
const found = userArray.find(user => user.username === "alice");

if (found) {
...
}

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 November 2025 21:14 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/4 "2025-11-03T21:14:16Z")

</div>

Welcome to the forum @alebaca

> [@alebaca](#):
>
> `for (i = 0; i < msg.payload.length; i=i+1)`

You need to declare the variable i  
for (let i=0; ...  
You need to put braces round the for code block

```auto
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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 November 2025 07:15 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/5 "2025-11-04T07:15:41Z")

</div>

Also need `==` for testing equality.

---

<div class="post-metadata">

**Author:** ![alebaca](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@alebaca](https://discourse.nodered.org/u/alebaca)\
**Post date:** [4 November 2025 19:59 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/6 "2025-11-04T19:59:08Z")

</div>

Thanks! It worked (after adding a `msg.` in front of `payload[i]idSala` I had also forgotten). Thanks for all your suggestions!

---

<div class="post-metadata">

**Author:** ![alebaca](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@alebaca](https://discourse.nodered.org/u/alebaca)\
**Post date:** [4 November 2025 20:00 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/7 "2025-11-04T20:00:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AllanOricil](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allanoricil/32/106911_2.png) [@AllanOricil](https://discourse.nodered.org/u/AllanOricil)\
**Post date:** [4 November 2025 21:29 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/8 "2025-11-04T21:29:01Z")

</div>

> [@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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 November 2025 21:48 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/9 "2025-11-04T21:48:01Z")

</div>

Wait till you see `i+=1` 😂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 November 2025 21:55 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/10 "2025-11-04T21:55:36Z")

</div>

Another alternative is to use array.some

```js
msg.payload = msg.payload.some(item => item.clients[0].idColegiat === "3554" && item.idSala === "3") ? 1 : 0;
return msg

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 November 2025 22:25 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/11 "2025-11-04T22:25:37Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![alebaca](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@alebaca](https://discourse.nodered.org/u/alebaca)\
**Post date:** [5 November 2025 20:26 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/12 "2025-11-05T20:26:45Z")

</div>

thanks all for your suggestions and comments!

---

<div class="post-metadata">

**Author:** ![alebaca](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@alebaca](https://discourse.nodered.org/u/alebaca)\
**Post date:** [13 November 2025 19:28 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/13 "2025-11-13T19:28:20Z")

</div>

I would like to post the whole node before closing the post. Thanks all for your contributions!

```auto
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)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 November 2025 20:10 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/14 "2025-11-13T20:10:22Z")

</div>

> [@alebaca](#):
>
> `&& msg.payload[i].idSala == "1")`

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:

```auto
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:

```auto
if (msg.payload[i].idSala === "1")

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 November 2025 20:10 UTC](https://discourse.nodered.org/t/loop-within-an-array/99572/15 "2025-11-27T20:10:56Z")

</div>

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