# Access number of array

**URL:** <https://discourse.nodered.org/t/access-number-of-array/81392>\
**Category:** General\
**Created:** [17 September 2023 09:37 UTC](https://discourse.nodered.org/t/access-number-of-array/81392 "2023-09-17T09:37:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [17 September 2023 09:37 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/1 "2023-09-17T09:37:34Z")

</div>

good Morning,  
and again I have a problem with reading out the data that I want to read out.  
In the picture I see the result: Payload: array[0]

I would like to respond to that!

e.g. If (the array has the number \> 0){  
Then do it}  
else  
{ //do nothing}

so far I have tried with msg.payload.length

I also output the payload length under the name payloadlaenge. The number 81 is there. Why? There should be 0 for comparison.

Where is my mistake in thinking?

Thanks in advance  
 ![Screenshot 2023-09-17 112535](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d0b27b47b5dac4e182f6ad2d0f991ea4b1d9616.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2023 09:43 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/2 "2023-09-17T09:43:52Z")

</div>

```auto
if(msg.payload.length === 0){
 do stuff....
}else{
   ... do something else
}

```

Arrays have a length property you can read about arrays here [Array - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#static_properties)  
[edit fixed typo in length.]  
If you wish a function to not output and do nothing return null, you can do this by setting `msg = null;`

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [17 September 2023 09:45 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/3 "2023-09-17T09:45:33Z")

</div>

> [@E1cid](#):
>
> `uff..`

thx for your fast replay! i test it ..

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [17 September 2023 09:56 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/4 "2023-09-17T09:56:16Z")

</div>

Unfortunately this did not work.  
There are only 2 possibilities which can be.  
In the first picture the array has a 0  
In the second picture the array has a 1

But the payload length never shows me 0 or 1.

![hat keinen inhalt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb29046c942e7da6173ed2bc012d8eb2b6d33af9.png)

![hat einen inhalt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b43ec5057bada56d1210e4a1467fb0eca633bed.png)

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [17 September 2023 11:04 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/5 "2023-09-17T11:04:24Z")

</div>

I think the error is that I don't need the length of the payload but the number of objects in the payload's array. Can this be?

To explain the whole thing a little better if necessary:  
I check in a database whether a data record already exists or not.

If the data record does not yet exist, I get the following in the debug window:  
object  
payload: array[0]

In this case the data record should be added.

If the data record already exists I get in the debug window:  
object  
payload: array[1]  
In this case, the data record already exists and nothing needs to be done.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2023 12:01 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/6 "2023-09-17T12:01:01Z")

</div>

If you use it correctly it will work  
see this example

```auto
[{"id":"d949b60aacba0c90","type":"inject","z":"b9860b4b9de8c8da","name":"array length 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"jsonata","x":330,"y":140,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"ee9b01abf97044ae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":80,"wires":[]},{"id":"837e4484190e5395","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"if(msg.payload.length === 0){\n msg.payload =\"length 0\";\n}else{\n msg.payload =\"length 1\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["9d0d9f4b63e71d9b"]]},{"id":"12a8804aedf9931f","type":"inject","z":"b9860b4b9de8c8da","name":"array legth 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"test\":1}]","payloadType":"jsonata","x":330,"y":180,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"9d0d9f4b63e71d9b","type":"debug","z":"b9860b4b9de8c8da","name":"debug 339","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":80,"wires":[]}]

```

Or to do nothing when array length is 1

```auto
[{"id":"d949b60aacba0c90","type":"inject","z":"b9860b4b9de8c8da","name":"array length 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"jsonata","x":330,"y":140,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"ee9b01abf97044ae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":80,"wires":[]},{"id":"837e4484190e5395","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"if(msg.payload.length === 0){\n msg.payload =\"length 0\";\n}else{\n msg = null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["9d0d9f4b63e71d9b"]]},{"id":"12a8804aedf9931f","type":"inject","z":"b9860b4b9de8c8da","name":"array legth 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"test\":1}]","payloadType":"jsonata","x":330,"y":180,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"9d0d9f4b63e71d9b","type":"debug","z":"b9860b4b9de8c8da","name":"debug 339","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":80,"wires":[]}]

```

[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)

 ![chrome_screenshot_1694952097751](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c3dc05b1a8b67ddf8af82bd74a26e99db56bb6b.png)

If you require more help please supply a test flow like i have that show the issue you are getting.

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [17 September 2023 12:18 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/7 "2023-09-17T12:18:29Z")

</div>

THANK YOU !

IT WORKS!

I have it a little bit changed

```auto
if (msg.payload.length === 0) {
    msg.topic = "INSERT INTO eq5q (link) VALUES ('" + msg.payloadpruef + "')";
    return msg;
} else {
    //mach nix
}

```

---

<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:** [17 September 2023 13:17 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/8 "2023-09-17T13:17:42Z")

</div>

Check whether your SQL variant has an INSERT IF NOT EXISTS feature. Using that will be much more efficient.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2023 13:39 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/9 "2023-09-17T13:39:47Z")

</div>

If you want to do it that way the else is superflulous, just do

```auto
if (msg.payload.length === 0) {
    msg.topic = "INSERT INTO eq5q (link) VALUES ('" + msg.payloadpruef + "')";
    return msg;
} 

```

---

<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:** [1 October 2023 13:40 UTC](https://discourse.nodered.org/t/access-number-of-array/81392/10 "2023-10-01T13:40:44Z")

</div>

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