# Allocate ID or a leading number to csv import

**URL:** <https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039>\
**Category:** General\
**Created:** [20 November 2021 21:05 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039 "2021-11-20T21:05:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [20 November 2021 21:05 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/1 "2021-11-20T21:05:56Z")

</div>

Hello!

I have a CSV file with about 1000 rows and 2 columns.

col1 col2

Is it possible that each line is given an ID or a leading number when importing.

Example.

Line 1: col1 col2 1  
Line 2: col1 col2 2  
Line 3: col1 col2 3

He should write the leading number in an object.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 November 2021 21:18 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/2 "2021-11-20T21:18:22Z")

</div>

you can use javascript's `map` command and add the id in the array of objects

`msg.payload = msg.payload.map((el, index) => { return { id: index + 1, ...el } }) return msg;`

**Example Flow:**

```auto
[{"id":"5a497eb3900dcd92","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":840,"wires":[["7dc1c5b4e73b459f"]]},{"id":"5f9fbd8a7188b524","type":"csv","z":"54efb553244c241f","name":"","sep":",","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"col1,col2","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":710,"y":840,"wires":[["1afc916faaef506b"]]},{"id":"7dc1c5b4e73b459f","type":"template","z":"54efb553244c241f","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"mustache","template":"1,2\n11,22\n111,222\n1111,2222","output":"str","x":520,"y":840,"wires":[["5f9fbd8a7188b524"]]},{"id":"1bb9aa311187c971","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":840,"wires":[]},{"id":"1afc916faaef506b","type":"function","z":"54efb553244c241f","name":"","func":"msg.payload = msg.payload.map((el, index) => { return { id: index + 1, ...el } })\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":860,"y":840,"wires":[["1bb9aa311187c971"]]}]

```

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [20 November 2021 21:23 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/3 "2021-11-20T21:23:53Z")

</div>

Hello!

Thank you so much for the answer.

that works great. Is it possible to add the total number of lines to it?

1/1000  
2/1000  
3/1000  
...

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 November 2021 21:26 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/4 "2021-11-20T21:26:25Z")

</div>

Sure ..

```auto
let total = msg.payload.length; 
msg.payload = msg.payload.map((el, index) => { return { id: index + 1, ...el, length: total } })

```

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [20 November 2021 21:28 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/5 "2021-11-20T21:28:23Z")

</div>

Thank you very much for that.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 November 2021 21:31 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/6 "2021-11-20T21:31:57Z")

</div>

you are welcome ..

some information on the methods used

[Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

[... spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)

---

<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:** [19 January 2022 21:32 UTC](https://discourse.nodered.org/t/allocate-id-or-a-leading-number-to-csv-import/54039/7 "2022-01-19T21:32:53Z")

</div>

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