# Looping through objects

**URL:** https://discourse.nodered.org/t/looping-through-objects/53578
**Category:** General
**Created:** [11 November 2021 14:39 UTC](https://discourse.nodered.org/t/looping-through-objects/53578 "2021-11-11T14:39:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lynq](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lynq](https://discourse.nodered.org/u/lynq)
#### Post date: [11 November 2021 14:39 UTC](https://discourse.nodered.org/t/looping-through-objects/53578/1 "2021-11-11T14:39:15Z")

</div>

Hello.

I've tried to find a way to do what I'm looking for but can't seem to figure it out.  
What i need to do is loop through "msg.payload.data.Nodes[1 through however many there are]" and search for key "Name" and Properties[0] or [1] Status.  
I've created a short test for it.  
The end goal is to save it as flow.set("Name","Status") from above, the "Node" can be anything from 1 through 154 at different times.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67edbbeea650f26faf00d492577685cea5e5d9c5.png)  
I've tried to sort it out in different ways, but the two things I'm coming closest to is something like this:  
(Either of the two)

```auto
/*for (var key in msg.payload.data.Nodes){
    var myObj = msg.payload.data.Nodes[key]
    
    msg.obj2 += myObj
msg.test += key
msg.payload = myObj*/
for (const [key, value] of Object.entries(msg.payload.data.Nodes[1])){
     msg.key += key   
    msg.val += value
       
    }
    
return msg;

```

The msg.key/val etc is just for checking the outputs.  
And as you can see in the top image, the first part always gets "undefined+key".  
If I change the msg.val from += to = so I only get the last object, it's working

Test-flow:

```auto
[{"id":"a21d0ac1.37a3e8","type":"debug","z":"649a18f3.49a458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1450,"y":460,"wires":[]},{"id":"b5700a0b.66f718","type":"function","z":"649a18f3.49a458","name":"","func":"/*for (var key in msg.payload){\n var myObj = msg.payload[key]\n \n msg.obj2 += myObj\nmsg.test += key\nmsg.payload = myObj*/\nfor (const [key, value] of Object.entries(msg.payload.data.Nodes[1])){\n msg.key += key \n msg.val += value\n \n }\n \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1120,"y":480,"wires":[["58948649.b189f8","a21d0ac1.37a3e8"]]},{"id":"cb8201a0.43b0c","type":"function","z":"649a18f3.49a458","name":"","func":"msg.payload = {data: {Nodes: {\n 1:{ \n Name:\"Test\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Status:\"Off\"\n }\n }\n \n },\n 2:{ \n Name:\"Test2\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Not:\"1\"\n }\n }\n \n },\n 3:{ \n Name:\"Test3\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Status:\"On\"\n }\n }\n \n }\n}\n \n \n \n}\n\n\n\n\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":890,"y":460,"wires":[["2edbe93d.724836","b5700a0b.66f718","a21d0ac1.37a3e8","a2848293.d9125"]]},{"id":"79983133.954c9","type":"inject","z":"649a18f3.49a458","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":710,"y":380,"wires":[["cb8201a0.43b0c"]]}]

```

---

<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: [11 November 2021 14:57 UTC](https://discourse.nodered.org/t/looping-through-objects/53578/2 "2021-11-11T14:57:53Z")

</div>

IT is `"msg.payload.data.Nodes` the "1" is the name of the first object in nodes.  
So you may only need to use `Object.values("msg.payload.data.Nodes)`.  
If you do not require the key "1" etc

---

<div class="post-metadata">

### Author: ![RootShell-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rootshell-coder/32/47167_2.png) [@RootShell-coder](https://discourse.nodered.org/u/RootShell-coder)
#### Post date: [11 November 2021 15:15 UTC](https://discourse.nodered.org/t/looping-through-objects/53578/3 "2021-11-11T15:15:18Z")

</div>

```auto
for (key in msg.payload.data.Nodes) {
    for (state in msg.payload.data.Nodes[String(key)].Properties) {
        if(msg.payload.data.Nodes[String(key)].Properties[String(state)].Status){
            flow.set(msg.payload.data.Nodes[String(key)].Name,
                     msg.payload.data.Nodes[String(key)].Properties[String(state)].Status)
    }
  }
}

```

```auto
[{"id":"a21d0ac1.37a3e8","type":"debug","z":"c991dbcde03824f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1568,"y":114,"wires":[]},{"id":"b5700a0b.66f718","type":"function","z":"c991dbcde03824f2","name":"loop obj","func":"/*for (var key in msg.payload){\n var myObj = msg.payload[key]\n \n msg.obj2 += myObj\nmsg.test += key\nmsg.payload = myObj*/\n/*for (const [key, value] of Object.entries(msg.payload.data.Nodes[1])){\n msg.key += key \n msg.val += value\n \n }\n \nreturn msg;\n*/\n\nfor (key in msg.payload.data.Nodes) {\n for (state in msg.payload.data.Nodes[String(key)].Properties) {\n if(msg.payload.data.Nodes[String(key)].Properties[String(state)].Status){\n flow.set(msg.payload.data.Nodes[String(key)].Name,\n msg.payload.data.Nodes[String(key)].Properties[String(state)].Status)\n }\n }\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1440,"y":114,"wires":[["a21d0ac1.37a3e8"]]},{"id":"cb8201a0.43b0c","type":"function","z":"c991dbcde03824f2","name":"","func":"msg.payload = {data: {Nodes: {\n 1:{ \n Name:\"Test\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Status:\"Off\"\n }\n }\n \n },\n 2:{ \n Name:\"Test2\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Not:\"1\"\n }\n }\n \n },\n 3:{ \n Name:\"Test3\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Status:\"On\"\n }\n }\n \n }\n}\n \n \n \n}\n\n\n\n\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1308,"y":114,"wires":[["b5700a0b.66f718"]]},{"id":"79983133.954c9","type":"inject","z":"c991dbcde03824f2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1154,"y":114,"wires":[["cb8201a0.43b0c"]]}]

```

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [11 November 2021 16:52 UTC](https://discourse.nodered.org/t/looping-through-objects/53578/4 "2021-11-11T16:52:32Z")

</div>

You can try this as well, this disregards where the status is not "On" or 'Off"

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

```auto
[
    {
        "id": "be9dbca190ca4687",
        "type": "tab",
        "label": "Flow 7",
        "disabled": false,
        "info": ""
    },
    {
        "id": "a21d0ac1.37a3e8",
        "type": "debug",
        "z": "be9dbca190ca4687",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 990,
        "y": 220,
        "wires": []
    },
    {
        "id": "cb8201a0.43b0c",
        "type": "function",
        "z": "be9dbca190ca4687",
        "name": "",
        "func": "msg.payload = {data: {Nodes: {\n 1:{ \n Name:\"Test\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Status:\"Off\"\n }\n }\n \n },\n 2:{ \n Name:\"Test2\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Not:\"1\"\n }\n }\n \n },\n 3:{ \n Name:\"Test3\",\n Properties:{\n 0:{\n 0:\"0\"\n },\n 1:{\n Status:\"On\"\n }\n }\n \n }\n}\n \n \n \n}\n\n\n\n\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 280,
        "y": 220,
        "wires": [
            [
                "cd6170b76258eec6"
            ]
        ]
    },
    {
        "id": "79983133.954c9",
        "type": "inject",
        "z": "be9dbca190ca4687",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 220,
        "wires": [
            [
                "cb8201a0.43b0c"
            ]
        ]
    },
    {
        "id": "cd6170b76258eec6",
        "type": "change",
        "z": "be9dbca190ca4687",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.data.Nodes",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 220,
        "wires": [
            [
                "914995cf5d692d96"
            ]
        ]
    },
    {
        "id": "914995cf5d692d96",
        "type": "split",
        "z": "be9dbca190ca4687",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 590,
        "y": 220,
        "wires": [
            [
                "0f99d41a8d97fc54"
            ]
        ]
    },
    {
        "id": "0f99d41a8d97fc54",
        "type": "switch",
        "z": "be9dbca190ca4687",
        "name": "",
        "property": "payload.Properties[\"1\"].Status",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "Off",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "On",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 710,
        "y": 220,
        "wires": [
            [
                "62eb5b3becb99671"
            ],
            [
                "62eb5b3becb99671"
            ]
        ]
    },
    {
        "id": "62eb5b3becb99671",
        "type": "function",
        "z": "be9dbca190ca4687",
        "name": "",
        "func": "flow.set(msg.payload.Name, msg.payload.Properties[\"1\"].Status)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 860,
        "y": 220,
        "wires": [
            [
                "a21d0ac1.37a3e8"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![lynq](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lynq](https://discourse.nodered.org/u/lynq)
#### Post date: [12 November 2021 06:29 UTC](https://discourse.nodered.org/t/looping-through-objects/53578/5 "2021-11-12T06:29:33Z")

</div>

Thanks a lot @RootShell-coder and @ScheepersJohan !  
Both of your solutions seems to work for me, I will have to try it with the live flow this weekend to make sure but it seems to do exactly what I want

---

<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: [26 November 2021 06:30 UTC](https://discourse.nodered.org/t/looping-through-objects/53578/6 "2021-11-26T06:30:19Z")

</div>

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