# Identify last column in a CSV

**URL:** <https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402>\
**Category:** General\
**Created:** [10 March 2023 18:44 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402 "2023-03-10T18:44:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)\
**Post date:** [10 March 2023 18:44 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/1 "2023-03-10T18:44:10Z")

</div>

I know the CSV node will use the column names as property names, and column contents as the property values; eg,

```auto
msg.payload : Object
object
col1: "first column content"
col2: "second column content"
col3: "third column content"
:
:

```

And also a list of the column names

```auto
columns: "col1,col2,col3,..."

```

But how would I determine the number of columns?

Or, specifically, determine which is the _ **last** _ column?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 19:21 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/2 "2023-03-10T19:21:10Z")

</div>

You can use the msg.columns and split it to to count the length, or pop() the last element for the name.  
e.g.

```auto
[{"id":"b3f55db2ad2b7341","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1820,"wires":[["dee2d518076c0f3b"]]},{"id":"dee2d518076c0f3b","type":"template","z":"65617ffeb779f51c","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1,2\n3,4\n","output":"str","x":320,"y":1820,"wires":[["c7e5c56d2b1fa550"]]},{"id":"c7e5c56d2b1fa550","type":"csv","z":"65617ffeb779f51c","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":470,"y":1820,"wires":[["db78d29828eedd96"]]},{"id":"db78d29828eedd96","type":"function","z":"65617ffeb779f51c","name":"function 22","func":"msg.payload.last = msg.columns.split(\",\").pop()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1880,"wires":[["5f7fa29e890ddf50"]]},{"id":"5f7fa29e890ddf50","type":"debug","z":"65617ffeb779f51c","name":"debug 242","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":1820,"wires":[]}]

```

```auto
msg.payload.last = msg.columns.split(",").pop()

```

---

<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:** [10 March 2023 19:39 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/3 "2023-03-10T19:39:32Z")

</div>

From the columns

```auto
const realCols = columns.split(',')
const lastCol = realCols[realCols.length - 1]

```

Or

```auto
const realCols = columns.split(',')
const lastCol = realCols.slice(-1)

```

From msg.payload

```auto
const colKeys = Object.keys(msg.payload)
const lastColContent = msg.payload[colKeys[colKeys.length - 1] ]
// or
const lastColContent1 = msg.payload[colKeys.slice(-1)]

```

There are always multiple ways to do things with JS.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 19:43 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/4 "2023-03-10T19:43:45Z")

</div>

Just to note Object.keys sometimes sorts the array.  
e.g.

 ![chrome_screenshot_1678477602896](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/7920e5faa02cb24f81e46ce73cb62ced29f6cab8.jpeg)

---

<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:** [10 March 2023 19:53 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/5 "2023-03-10T19:53:44Z")

</div>

> [@E1cid](#):
>
> Just to note Object.keys sometimes sorts the array.

I believe that's because you set the keys to be integers. Odd things happen then:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b16d664cbe0505155497a968ce703831ca6cdc76.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1dd5051573c2558e7544aa8cd2e4f9bc63260af8.png)

Bottom line is avoid using integer keys in an object. But yes, you are correct. All versions of JS as far as I know treat the keys in the expected order when they are strings.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 March 2023 19:57 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/6 "2023-03-10T19:57:27Z")

</div>

Yes I know that, but worth a note as some time i use integer as keys for decimal to string lookup tables.

---

<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:** [24 April 2023 09:32 UTC](https://discourse.nodered.org/t/identify-last-column-in-a-csv/76402/7 "2023-04-24T09:32:25Z")

</div>

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