# Problem while depending of object length

**URL:** <https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678>\
**Category:** General\
**Tags:** function-node\
**Created:** [23 April 2022 09:46 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678 "2022-04-23T09:46:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Licky](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@Licky](https://discourse.nodered.org/u/Licky)\
**Post date:** [23 April 2022 09:46 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/1 "2022-04-23T09:46:22Z")

</div>

Hello, i'm receiving data which change in every objects, i'd like to make a loop and send 60 differents msg if there's 60 objects for example.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/eea51191f1d97c29fcb0704ec6a69f43507e3c04.png)

this is the code i've done, i managed to get the amount of objects by using the msg.payload.length, and in the new msg.payload i'd like to send what you can see in the screenshot, but just change the "x" by the number of the object, to get the "lat" and "lon" variables changing depending of the objects.

The message get sent only once only with the data of the msg.payload[0] object.

How my msg.payload first look :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6eaaa18918181fd75650475df60268225262340f.png)

Any help would be really apreciated ! Thank you for reading me and i apologize for my english.  
My flow code as well:

```auto
[
    {
        "id": "67991373.45438c",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "4440c71e.f88f18",
        "type": "ui_table",
        "z": "67991373.45438c",
        "group": "fe9b4de4.dcd72",
        "name": "Tableau",
        "order": 11,
        "width": "25",
        "height": "14",
        "columns": [
            {
                "field": "N° ID",
                "title": "N° ID",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Plate-forme",
                "title": "Plate-forme",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Date Msg",
                "title": "Date Msg",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Longitude",
                "title": "Longitude",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Latitude",
                "title": "Latitude",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Altitude",
                "title": "Altitude",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Date Msg",
                "title": "Date Msg",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            }
        ],
        "outputs": 0,
        "cts": false,
        "x": 840,
        "y": 60,
        "wires": [],
        "info": "test"
    },
    {
        "id": "6681b08b.4a688",
        "type": "file in",
        "z": "67991373.45438c",
        "name": "Read File",
        "filename": "/home/pi/Documents/Python/CSV/ArgosData.csv",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 440,
        "y": 60,
        "wires": [
            [
                "e843fa29.885718"
            ]
        ]
    },
    {
        "id": "9a7a7971.dbc4b8",
        "type": "inject",
        "z": "67991373.45438c",
        "name": "",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 300,
        "y": 60,
        "wires": [
            [
                "6681b08b.4a688"
            ]
        ]
    },
    {
        "id": "e843fa29.885718",
        "type": "csv",
        "z": "67991373.45438c",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "none",
        "multi": "mult",
        "ret": "\\r\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 590,
        "y": 60,
        "wires": [
            [
                "8476966506776ebb",
                "4440c71e.f88f18",
                "7b6e51f2bfdfb646"
            ]
        ]
    },
    {
        "id": "8476966506776ebb",
        "type": "debug",
        "z": "67991373.45438c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1030,
        "y": 60,
        "wires": []
    },
    {
        "id": "68524531a6a4f97b",
        "type": "comment",
        "z": "67991373.45438c",
        "name": "Problème : ",
        "info": "Impossible de mettre :\n\n`Date de loc.\nSat.\nQualité loc.`\n\nDans le tableau, raison probable = un point après le nom de l'objet qui fait buger lors du transfert du fichier CSV -> Tableau ",
        "x": 840,
        "y": 20,
        "wires": []
    },
    {
        "id": "7688e1b1256b496d",
        "type": "ui_worldmap",
        "z": "67991373.45438c",
        "group": "2f22af1f9a5f76b7",
        "order": 0,
        "width": 0,
        "height": 0,
        "name": "",
        "lat": "43.92962985967133",
        "lon": "2.1338488251496166",
        "zoom": "",
        "layer": "OSMG",
        "cluster": "",
        "maxage": "",
        "usermenu": "hide",
        "layers": "hide",
        "panit": "false",
        "panlock": "false",
        "zoomlock": "false",
        "hiderightclick": "true",
        "coords": "none",
        "showgrid": "false",
        "allowFileDrop": "false",
        "path": "/worldmap",
        "overlist": "DR,CO,RA,DN,HM",
        "maplist": "OSMG,OSMC,EsriC,EsriS,EsriT,EsriDG,UKOS",
        "mapname": "",
        "mapurl": "",
        "mapopt": "",
        "mapwms": false,
        "x": 1000,
        "y": 160,
        "wires": []
    },
    {
        "id": "7b6e51f2bfdfb646",
        "type": "function",
        "z": "67991373.45438c",
        "name": "",
        "func": "let values = msg.payload.length;\nx=0;\n\nwhile (x < values) {\nmsg.payload=\n{\n\"values=\":values,\n\"name\":\"TIPOD1\",\n\"label\":\"msg.payload.DateMsg\",\n\"lat\":msg.payload[x].Latitude,\n\"lon\":msg.payload[x].Longitude,\n\"icon\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lateral_buoy_r.svg/188px-Lateral_buoy_r.svg.png?20060717200653\"\n}\nreturn msg;\nx=x+1;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 160,
        "wires": [
            [
                "e3d564c357cb9798"
            ]
        ]
    },
    {
        "id": "e3d564c357cb9798",
        "type": "debug",
        "z": "67991373.45438c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 160,
        "wires": []
    },
    {
        "id": "fe9b4de4.dcd72",
        "type": "ui_group",
        "name": "Data reçues ",
        "tab": "da612906.998368",
        "order": 1,
        "disp": true,
        "width": "25",
        "collapse": false
    },
    {
        "id": "2f22af1f9a5f76b7",
        "type": "ui_group",
        "name": "WorldMap",
        "tab": "b4d8e0b4b8e5de10",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "da612906.998368",
        "type": "ui_tab",
        "name": "Donnee",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "b4d8e0b4b8e5de10",
        "type": "ui_tab",
        "name": "World Map",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<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:** [23 April 2022 09:56 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/2 "2022-04-23T09:56:28Z")

</div>

> [@Licky](#):
>
> The message get sent only once only with the data of the msg.payload[0] object.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a1fa98ce414e09c9608e734b3bef3ff07695f9f.png)

ALSO: you are calling `return msg` so the loop never continues.

  

You need to store your array somewhere before altering `msg.payload` e.g...

```auto
const count = msg.payload.length;
const values = msg.payload;

for (let index = 0; index < values.length; index++) {
    const entry = values[index];
    msg.payload = {
        "values=": count,
        "name": "TIPOD1",
        "label": "msg.payload.DateMsg",
        "lat": entry.Latitude,
        "lon": entry.Longitude,
        "icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lateral_buoy_r.svg/188px-Lateral_buoy_r.svg.png?20060717200653"
    }
    node.send(msg);
}

```

---

<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:** [23 April 2022 09:59 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/3 "2022-04-23T09:59:06Z")

</div>

PS, if you use monaco editor, you get a whole bunch of help in the function node.

your code in monaco...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93d38b24091820affeff9489e00f14ade7615463.png)  
... it lights up like a christmas tree.

Monaco is enabled in `settings.js`

---

<div class="post-metadata">

**Author:** ![Licky](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@Licky](https://discourse.nodered.org/u/Licky)\
**Post date:** [23 April 2022 14:56 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/4 "2022-04-23T14:56:12Z")

</div>

> [@Licky](#):
>
> Hello, thank you a lot for the fast answer, i kinda understand where was the problem now. But i'm confused about the "index" you use in the for and values. What does this Index correspond for?
> 
> Again, thank you a lot for taking the time to explain everything !

Nevermind, i found my answer, i didn't read the code corretly. Anyways, it's working fine now , thanks again and i wish you a good weekend!

---

<div class="post-metadata">

**Author:** ![Licky](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@Licky](https://discourse.nodered.org/u/Licky)\
**Post date:** [23 April 2022 15:11 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/5 "2022-04-23T15:11:08Z")

</div>

Before i go, i got another small question too..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d27b901c896679cd33e7d8a8344819c658aee09.png)

The parameter i need to use is "date msg" with the space between or it doesn't fit well with what the object send, but then the syntax is kinda broken, like in the example above, is there any way to fix that too?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/738ebb1a4783fa2cd48d49befdd818233b2ef667.png)

---

<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:** [23 April 2022 16:28 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/6 "2022-04-23T16:28:43Z")

</div>

You need to use bracket notation for variables with invalid characters in. E  
G. `entry["Date 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:** [7 May 2022 16:28 UTC](https://discourse.nodered.org/t/problem-while-depending-of-object-length/61678/8 "2022-05-07T16:28:52Z")

</div>

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