Extract a part of an object, convert to string and number

Hi All
I am using node red 3.0.3 and node js v18.16.0 in a raspberry pi. A flow storage (gravity sensor) in my program is generating an object with many fields. I want to know how can I get one of the fields ,this field should be number, so I guess that the field should be converted to a integer. How can I do that?
This is the object structure (in fact has more fields), the only field that I need is called "minor"
object
uuid: "a495bb50c5b14b44b5121370f02d74de"
major: 999
minor: 1004
tx_power: -103
rssi: -63
mac: "c9:23:5f:60:d3:23"
Color: "ORANGE"
image

image

Ok, not impossible.

But you need to post the data better.

Where you posted the message:
Before pasting the code, click the </> button at the top (Or Control e)

type or paste code here

And paste the code/message where it says.

I'm guessing the part you want is msg.minor, but can not be sure.

Put a function node after the node that gives you the above message and basically you make it:

const msg.payload = parseInt(msg.minor);
return msg;

Then the next node gets a payload with an integer value which is what msg.minor was in the previous message.

According to you image minor is already a number (shows blue in debug) If you hover your mouse over the minor name you will see three icons. One is copy path, If you copy it and paste it in a change node you can move the value where ever you wish.
In a change node to move it to msg.payload
set msg payload
to value of msg. payload.minor

You may get some benefit from watching the
essentials videos

1 Like

Thanks a lot, it worked

var res = Number(str);
res=res*1000;
msg.payload = res;
return msg;

In case you did not realise, you can shorten that to

msg.payload = Number(str)*1000
return msg

Thanks . it works

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