# Split a Lat/Lon coordinates from a string

**URL:** <https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005>\
**Category:** General\
**Created:** [4 August 2022 08:52 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005 "2022-08-04T08:52:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 08:52 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/1 "2022-08-04T08:52:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2022 08:57 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/2 "2022-08-04T08:57:19Z")

</div>

Do it all in a function node.

```auto
const splitLoc = msg.payload.split(' ')
msg.lat = splitLoc[0]
msg.lon = splitLoc[1]
return msg

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 August 2022 08:59 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/3 "2022-08-04T08:59:27Z")

</div>

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".

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2022 09:00 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/4 "2022-08-04T09:00:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 09:01 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/5 "2022-08-04T09:01:24Z")

</div>

> [@TotallyInformation](#):
>
> node

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?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 August 2022 09:01 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/6 "2022-08-04T09:01:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 09:01 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/7 "2022-08-04T09:01:45Z")

</div>

Thanks anyway 👍

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2022 09:02 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/8 "2022-08-04T09:02:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 09:05 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/9 "2022-08-04T09:05:31Z")

</div>

> [@TotallyInformation](#):
>
> reset the payload

Ahh, doing that gives me an empty array...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/afb3ce156a3563e886237a70773164d739a94d34.png)  
I guess I have to put part of the earlier syntax into the braces?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2022 09:07 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/10 "2022-08-04T09:07:57Z")

</div>

> [@dougle03](#):
>
> Ahh, doing that gives me an empty array...

That is what you've told it to do 😉

> [@dougle03](#):
>
> I guess I have to put part of the earlier syntax into the braces?

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 09:10 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/11 "2022-08-04T09:10:54Z")

</div>

So, I changed the `msg.lat/lon` to variables `var` and included them in the array.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/3005ff7dec10e44898271f45535788bfe8f7a417.png)

Now I get them in the array.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a81067548e72def3627fb7e9f684937719693fa6.png)

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?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 August 2022 09:16 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/12 "2022-08-04T09:16:52Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 August 2022 09:26 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/13 "2022-08-04T09:26:58Z")

</div>

> [@dougle03](#):
>
> Now I get them in the array.

That is an object not an array.

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 09:27 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/14 "2022-08-04T09:27:10Z")

</div>

> [@dynamicdave](#):
>
> Whatever you put between the single or double quotes is what the split will be based on.

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...

> [@dynamicdave](#):
>
> `const` has nothing to do with the `split` function - it just defines the type of variable.

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 09:28 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/15 "2022-08-04T09:28:09Z")

</div>

> [@Colin](#):
>
> That is an object not an array.

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

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 August 2022 09:51 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/16 "2022-08-04T09:51:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dougle03](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dougle03/32/32979_2.png) [@dougle03](https://discourse.nodered.org/u/dougle03)\
**Post date:** [4 August 2022 10:25 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/17 "2022-08-04T10:25:59Z")

</div>

> [@dynamicdave](#):
>
> I keep forgetting things

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

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 August 2022 11:05 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/18 "2022-08-04T11:05:12Z")

</div>

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?"** _

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 August 2022 11:25 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/19 "2022-08-04T11:25:03Z")

</div>

> [@dynamicdave](#):
>
> That's nothing - I'm 76 and STILL learning (problem is, I keep forgetting things).

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

> [@dougle03](#):
>
> That's my problem too.. Use it or lose it I think the phrase is... lol

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.

> [@dynamicdave](#):
>
> Node-RED is so **addictive**

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.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [4 August 2022 21:13 UTC](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005/20 "2022-08-04T21:13:19Z")

</div>

> [@dynamicdave](#):
>
> Node-RED is so **addictive**

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."

[Next page](https://discourse.nodered.org/t/split-a-lat-lon-coordinates-from-a-string/66005.md?page=2)
