# Reading Object in loop

**URL:** <https://discourse.nodered.org/t/reading-object-in-loop/33342>\
**Category:** General\
**Created:** [25 September 2020 11:27 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342 "2020-09-25T11:27:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward](https://avatars.discourse-cdn.com/v4/letter/w/439d5e/32.png) [@Ward](https://discourse.nodered.org/u/Ward)\
**Post date:** [25 September 2020 11:27 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/1 "2020-09-25T11:27:09Z")

</div>

Hi

I'm trying to read every line in a object through a loop function. The object looks like this.  
 ![Object](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/829c6071519036a687d09d91ff733d0629ec35ac.png)

So i tried the following code (also tried with '1'), and to read it directly msg.payload.1 doesn't works because it expexted a string and not a number:  
 ![Object read 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c85d99123dd8228ae57b387e5c1c9ebf6e2a046.png)

I pretty new in javascript but i thought the $ meant that the word after have to read as a variable. Or i'm i wrong? Because i also tried with a object with words instead of numbers but still doesn't works.

![Object read one](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/2575272c623d78077718c44efb918ac068afde28.png)

Thanks at advance!  
Kind regards,  
Ward

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 September 2020 11:32 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/2 "2020-09-25T11:32:44Z")

</div>

> [@Ward](#):
>
> msg.payload.1

Try `msg.payload['1']`

---

<div class="post-metadata">

**Author:** ![Ward](https://avatars.discourse-cdn.com/v4/letter/w/439d5e/32.png) [@Ward](https://discourse.nodered.org/u/Ward)\
**Post date:** [25 September 2020 11:50 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/3 "2020-09-25T11:50:19Z")

</div>

@UnborN thanks for the reply!  
I't doesn't work either it says.  
' Expected an assignment or function call and instead saw an expression'.

Isn't meant for an array?  
So i search for a way to change my object in a array and found the following:  
msg.entries = Object.entries(msg.payload);  
msg.values = Object.values(msg.payload);  
msg.keys = Object.keys(msg.payload);

And with this i can do what i first wanted to do.

But if there is a easy way i can select the line without converting my object to an array please let me know.

Kind regards,  
Ward

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 September 2020 12:05 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/4 "2020-09-25T12:05:44Z")

</div>

Those Object commands come in handy when you are looping but i didnt see any loops in your fuctions above.

Test flow :

```auto
[{"id":"fad7142c.8cf118","type":"inject","z":"a311c276.8fe21","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":false,\"2\":true,\"3\":false}","payloadType":"json","x":430,"y":1700,"wires":[["211e4649.64d8a2","fb642a73.c1be98"]]},{"id":"211e4649.64d8a2","type":"debug","z":"a311c276.8fe21","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload['1']","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":1760,"wires":[]},{"id":"fb642a73.c1be98","type":"debug","z":"a311c276.8fe21","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1660,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Ward](https://avatars.discourse-cdn.com/v4/letter/w/439d5e/32.png) [@Ward](https://discourse.nodered.org/u/Ward)\
**Post date:** [25 September 2020 12:14 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/5 "2020-09-25T12:14:19Z")

</div>

hi @UnborN. My apologies i probably typed something wrong. Because i tried the syntax again and it does works now.

There is indeed no loop in my function yet. I was first trying to read the correct line manually but now i now how it works i can start with the loop.

Thanks for the solution!

Kind regards  
Ward

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 September 2020 12:22 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/6 "2020-09-25T12:22:06Z")

</div>

great 😉 .. if you dont have many properties/keys in your object you can read them one by one.

loops are useful and you can do something like :

```auto
let values = Object.values(msg.payload);

values.forEach((val,index) => {
 // do something ..
})

```

[forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)

---

<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:** [9 October 2020 12:22 UTC](https://discourse.nodered.org/t/reading-object-in-loop/33342/7 "2020-10-09T12:22:19Z")

</div>

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