# Grabbing last character in a string in function node

**URL:** <https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683>\
**Category:** General\
**Created:** [7 July 2020 20:49 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683 "2020-07-07T20:49:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlturner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vlturner/32/15211_2.png) [@vlturner](https://discourse.nodered.org/u/vlturner)\
**Post date:** [7 July 2020 20:49 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/1 "2020-07-07T20:49:51Z")

</div>

I'm having a problem grabbing the last character of my msg.port. I want to use it to inject it into the right column of a csv file.

timestamp,port or sensor,pressure\_0,pressure\_1, temperature.

I did something stupid and cant seem to get the last character of the port grabbed.

```auto
[{"id":"f77e447e.b74878","type":"csv","z":"ea882813.8a2a08","name":"","sep":",","hdrin":true,"hdrout":false,"multi":"mult","ret":"\\n","temp":"time,port,pressure_0,pressure_1,temperature","skip":"0","strings":false,"x":770,"y":360,"wires":[["8df2b3b.e81e0d"]]}]

```

I am writing to a file and cant seem to get the first header row of the csv file written correctly either with the CSV node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef869436e76220618bd62ec6cb98c185d7681d7f.png)

Added the slice function but still not quite right.

```auto
[{"id":"ec4bbb9f.3bd4d8","type":"function","z":"ea882813.8a2a08","name":"javascript","func":"var date;\ndate = new Date();\ndate = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2) + ':' + ('00' + date.getMilliseconds()).slice(-2));\npid = (\"pressure_\" + msg.port.slice(-1));\npressure = msg.payload;\npayload = {\"time\":date, \"port\":msg.port, pid:pressure};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":320,"wires":[["f77e447e.b74878"]]},{"id":"6e7a58b8.d2afc8","type":"inject","z":"ea882813.8a2a08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":280,"wires":[["69b01c88.337244"]]},{"id":"69b01c88.337244","type":"change","z":"ea882813.8a2a08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"14.5","tot":"str"},{"t":"set","p":"port","pt":"msg","to":"/dev/ttyUSB0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":280,"wires":[["ec4bbb9f.3bd4d8"]]},{"id":"8df2b3b.e81e0d","type":"debug","z":"ea882813.8a2a08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":360,"wires":[]},{"id":"55d1ab98.eeef2c","type":"inject","z":"ea882813.8a2a08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":320,"wires":[["e010d9d5.7383c8"]]},{"id":"e010d9d5.7383c8","type":"change","z":"ea882813.8a2a08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"14.5","tot":"str"},{"t":"set","p":"port","pt":"msg","to":"/dev/ttyUSB1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":320,"wires":[["ec4bbb9f.3bd4d8"]]},{"id":"5dc563c5.07c90c","type":"inject","z":"ea882813.8a2a08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":360,"wires":[["8f6fef77.5a3388"]]},{"id":"8f6fef77.5a3388","type":"change","z":"ea882813.8a2a08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"75.5","tot":"str"},{"t":"set","p":"port","pt":"msg","to":"MCP9808","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":360,"wires":[["2cc0fee1.4186a2"]]},{"id":"2cc0fee1.4186a2","type":"function","z":"ea882813.8a2a08","name":"javascript","func":"var date;\ndate = new Date();\ndate = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2) + ':' + ('00' + date.getMilliseconds()).slice(-2));\npayload = {\"time\":date, \"port\":\"MCP9808\", \"temperature\":msg.payload};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":360,"wires":[["f77e447e.b74878"]]},{"id":"f77e447e.b74878","type":"csv","z":"ea882813.8a2a08","name":"","sep":",","hdrin":true,"hdrout":false,"multi":"mult","ret":"\\n","temp":"time,port,pressure_0,pressure_1,temperature","skip":"0","strings":false,"x":770,"y":360,"wires":[["8df2b3b.e81e0d"]]}]

```

I need to create the variables pressure0 and pressure1 so depending in the inbound messages port it is either /dev/ttyUSB0 or /dev/ttyUSB1

```auto
var date;
date = new Date();
date = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2) + ':' + ('00' + date.getMilliseconds()).slice(-2));
pid = ("pressure_" + msg.port.slice(-1));
pressure = msg.payload;
payload = {"time":date, "port":msg.port, "pressure"(pid):pressure};
msg.payload = payload;
return msg;

```

---

<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:** [7 July 2020 21:03 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/2 "2020-07-07T21:03:58Z")

</div>

The last character of a string can be obtained several ways such as `'this is a string'.slice(-1)`.

To output to a CSV, I think the msg.payload needs to be an array of objects where each object has a property for each column. Possibly you could get away with an array of array's but I've not tried it.

---

<div class="post-metadata">

**Author:** ![vlturner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vlturner/32/15211_2.png) [@vlturner](https://discourse.nodered.org/u/vlturner)\
**Post date:** [8 July 2020 06:34 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/3 "2020-07-08T06:34:35Z")

</div>

@hotNipi Any suggestion? I have been working so hard that I messed up.

I can't remember how to pull this together.

```auto
"pressure"(pid):pressure}

```

it should print pressure0:14.5 or pressure1:14.5 so it will go in the right csv column.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [8 July 2020 07:17 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/4 "2020-07-08T07:17:27Z")

</div>

ES6 gives some ways to make dynamic properties

```auto
msg.payload = {"time":date, "port":msg.port, [`pressure${msg.port.slice(-1)}`]:pressure};

```

---

<div class="post-metadata">

**Author:** ![vlturner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vlturner/32/15211_2.png) [@vlturner](https://discourse.nodered.org/u/vlturner)\
**Post date:** [8 July 2020 18:51 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/5 "2020-07-08T18:51:18Z")

</div>

You da man....Lets see [`{()}`]...Got it...Can't we find a way to incorporate |/!@#\_-+ It's just not quite convoluted enough. We need that "Over the Top" garnish.

Thanks you... 🙂

---

<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:** [8 July 2020 21:05 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/6 "2020-07-08T21:05:53Z")

</div>

JavaScript is far from the worst culprit in that sense 😄

---

<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:** [6 September 2020 21:18 UTC](https://discourse.nodered.org/t/grabbing-last-character-in-a-string-in-function-node/29683/7 "2020-09-06T21:18:54Z")

</div>

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