# Split a Lat/Lon coordinates from a string

String: 53.0094583 -1.2078455

I'm playing around with the split node using space as the splitter, but then I need to put the two parts into a single message as msg.lat and msg.lon.
What's the simplest method? Send each to a variable then use a function node to re-combine the two messages into a single one? I'm thinking there might be a simpler way.
TIA

Do it all in a function node.

msg.lat = splitLoc[0]
msg.lon = splitLoc[1]
return msg

Damn - you are too quick. I was just typing my response (same as yours) and you beat me to it.
I suppose that's something to do with "youth" compared with "old age".

3 Likes

haha, and I've one hand in a pot!

2 Likes

Awesome, works well thanks. If I want to put the lat and lon in an array under msg.payload, would I just add {} around the function, with a , between them?

Ha, ha - I forgot you had injured your hand. That just makes the slowness of my response even worst!!

1 Like

Thanks anyway

Yes, or once you've captured the split, you can reset the payload to an object with msg.payload = {} then adjust the two assignments accordingly.

Ahh, doing that gives me an empty array...

I guess I have to put part of the earlier syntax into the braces?

That is what you've told it to do

Line 4 would need to be after line 1. Lines 2 & 3 need changing to include the payload bit: msg.payload.lat

1 Like

So, I changed the msg.lat/lon to variables var and included them in the array.

Now I get them in the array.

I need to investigate the const function, I have no idea how it split the two values out, unless the space in the "" was used as the delimiter? If so, I assume its a version of the split node?

const has nothing to do with the split function - it just defines the type of variable.

Julian could have just as easily used... var or let

It's just a convention that people use for variables that aren't going to change.
That sounds a bit weird... variables that aren't going to change.

You are correct in the second part of your response. Whatever you put between the single or double quotes is what the split will be based on.

2 Likes

That is an object not an array.

1 Like

Ahh makes sense now then. So the [0] [1] are because we know there is only one space in the string, thus can only be two outputs in the array.
So the .split does the splitting...

That makes sense too. I've always used var, I forget there are other types that can be used.

Ahh, I'm still learning. At 50 and not from a coding background, every lesson is hard work.. lol

1 Like

That's nothing - I'm 76 and STILL learning (problem is, I keep forgetting things).

3 Likes

That's my problem too.. Use it or lose it I think the phrase is... lol

The other problem is... Node-RED is so addictive, once you get up the learning curve and discover how quickly and easily you can create a flow to control something; report something; display something.
It's also great to see what other people have done and think...
"now why didn't I think of that clever idea?"

1 Like

Haha, I'm 62 and certainly constantly forgetting and learning. It's what keeps us young at heart

I do recommend finding a short JavaScript course or two to run through, it really helps cement some of the basics and gives you a better grounding for doing internet searches for all the things you wont remember. I forever have to look up simple things because I'm not a full-time dev and haven't been for many decades now. Thankfully JS has some great resources such as the fantastic Mozilla Developer Network (MDN) which is the go-to place for the JS language, both for references and for tutorials/training. Microsoft also have a nice and free online JS course.

How very true! It lets you be so creative as it takes care of most of the boring bits for you.

Programming is my personal creative outlet. I can't sew like my wife can, and I'm generally rubbish with my hands, so programming is it.

4 Likes

Indeed! JavaScript, on the other hand, reminds me of the old saying, "Never wrestle with a pig. You both get dirty and the pig enjoys it."

2 Likes