Reading Object in loop


I'm trying to read every line in a object through a loop function. The object looks like this.

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

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

Thanks at advance!
Kind regards,

Try msg.payload['1']

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

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

Test flow :


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

great :wink: .. 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 :

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

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


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