# Global set objects from csv Rows of configuration file

**URL:** <https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479>\
**Category:** Industrial\
**Created:** [5 September 2020 17:56 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479 "2020-09-05T17:56:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Letis009](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Letis009](https://discourse.nodered.org/u/Letis009)\
**Post date:** [5 September 2020 17:56 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/1 "2020-09-05T17:56:33Z")

</div>

I am sure this is a basic coding principle but i am unable to find a forum thread for the problem  
I receive (tcp) data with a device number and need to correlate it to an ip and a name  
We want to load the parameters to memory to speed up the process because mysql is slower then we would like

Imagine 3 devices a,d,g  
We have a csv with parameters for devices  
For example  
number,ip,name  
a,b,c  
d,e,f  
g,h,i

flow is currently  
Inject=\> file-in=\> csv=\> change  
(Set global.number to msg.payload)  
This gives me an object in global context with all the parameters for g,h,i only  
Makes sense as each line would over ride the previous

What is the syntax for iterating something like  
From csv node into function node  
var i = msg.payload.col1;  
global.set(“$col1”,i);  
// above would only give me global.$col1 of “g”

I imagine it is a forEach but so far way above my pay grade

Use case has 22 columns of parameters and and 100 rows of devices (up to 500 max) so individual entries would not be viable

---

<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:** [5 September 2020 19:21 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/2 "2020-09-05T19:21:36Z")

</div>

> [@Letis009](#):
>
> I receive (tcp) data with a device number and need to correlate it to an ip and a name

One way to organise it would be a sparse array where the index is the number and the ip and name are in the array element. So to initialise it, if you were writing code, you might do something like

```auto
let table = []
table[735] = {ip: "192.168.16.4", name: "192.168.16.5}
table[832] = {ip: "192.168.16.4", name: "192.168.16.5}

```

and so on. Then when you have a device number and want the ip and name you can use `table[number].ip` and `table[number].name`.

You could read your csv file on startup and build the global table by picking up each line of the file and adding them into the table.  
As for how to do that then read in the file a line at a time and send it to a function node, in the function node then pick up the table from global context and add the new element in.

---

<div class="post-metadata">

**Author:** ![Letis009](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Letis009](https://discourse.nodered.org/u/Letis009)\
**Post date:** [5 September 2020 19:25 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/3 "2020-09-05T19:25:11Z")

</div>

Thank you Colin  
I appreciate the insight  
Will test and come back to results

---

<div class="post-metadata">

**Author:** ![Letis009](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Letis009](https://discourse.nodered.org/u/Letis009)\
**Post date:** [5 September 2020 19:36 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/4 "2020-09-05T19:36:42Z")

</div>

You have answered what would have been a question later . How to retrieve the table entries

With ...table[832] = ...  
Can I replace “832“ with a variable like msg.payload.col1 ?  
To start a loop to read in entire csv , one message per line

---

<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:** [5 September 2020 19:39 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/5 "2020-09-05T19:39:56Z")

</div>

Yes, that is what I was attempting to suggest.

**[Edit]** But if it is coming in as a string use `table[Number(msg.payload.col1)] = ...`

---

<div class="post-metadata">

**Author:** ![Letis009](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Letis009](https://discourse.nodered.org/u/Letis009)\
**Post date:** [6 September 2020 09:43 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/6 "2020-09-06T09:43:39Z")

</div>

your solution works nicely  
just need to put table into global context but that is another thread  
Thanks Again

---

<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:** [20 September 2020 09:43 UTC](https://discourse.nodered.org/t/global-set-objects-from-csv-rows-of-configuration-file/32479/7 "2020-09-20T09:43:42Z")

</div>

Closed on 20 September 2020 09:43 UTC

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