# Global.set data without creating an array

**URL:** <https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899>\
**Category:** General\
**Created:** [24 April 2023 21:29 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899 "2023-04-24T21:29:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [24 April 2023 21:29 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/1 "2023-04-24T21:29:00Z")

</div>

Hi!  
I want to store some data in the global context data.  
With the help of an import job (.json file \> global context storage) I have set up a structure of e.g.:

config.functionNr.200.context

And, yes, the "200" is an object, not part of an array. I can get the data via:

```auto
global.get('config.functionNr.200.context');

```

Now, when I write some new content as context data, using

```auto
global.set('config.functionNr.200.context', value);

```

everything works fine, but when I use

```auto
let number = global.get('some.context.variable.as.number');
global.set('config.functionNr.' + number + '.context', value);

```

I get an array with 199 empty values and one last value with my data.

Can you help me to find out my mistake?

Thanks a lot!  
Florian

---

<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:** [24 April 2023 21:47 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/2 "2023-04-24T21:47:42Z")

</div>

Honestly, I wouldn't bother trying to get/set deep into the object. Just grab the whole object. I seem to remember we did some tests quite a while ago and came to the conclusion that the whole variable is in memory anyway so there is no advantage trying to only get part of it.

Once you have the whole thing, you can use bracket notation to access via variables.

```auto
const myconf = global.get('config')
myconf.functionNr.200.context = value
myconf.functionNr[mynum].context = value

global.set('config', myconf)

```

You might need to check whether your number is really a number or a string though.

Also note that you should probably try to avoid calling things by names that might already be used such as `config` and `number`.

---

<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:** [24 April 2023 22:13 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/3 "2023-04-24T22:13:09Z")

</div>

Or use square bracket notation in the path string

```auto
let number = 20;
global.set('config.functionNr["' + number + '"].context', "value");

```

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [24 April 2023 22:14 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/4 "2023-04-24T22:14:39Z")

</div>

Hi Julian,

thanks for your reply.  
You're right, in the original flow the names are not set to any names like "number"...

It's a flow where I try to manipulate single values of a huge object containing plenty of data, ordered in "named objects" like this:

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

and if you try to dive in to the values, if they would be ordered as arrays, you won't be happy...

In the meantime I think I found out that the problem is the ".context" (which is named different in real data) after the number. When I do it like:

```auto
let theValue = {context: "firstcontext", context2: "secondcontext"};
global.set('myconf.functionNr.200', theValue);

```

I can get what I want to get!

---

<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:** [24 April 2023 22:21 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/5 "2023-04-24T22:21:49Z")

</div>

> [@TotallyInformation](#):
>
> Honestly, I wouldn't bother trying to get/set deep into the object. Just grab the whole object. I seem to remember we did some tests quite a while ago and came to the conclusion that the whole variable is in memory anyway so there is no advantage trying to only get part of it.

My testing suggested that writing straight to the context property is approx 10% faster than reading the whole context object, then setting a property, then writing the whole context object.  
Reading the whole context object then selecting an object property is quicker then reading a single context object property though.

---

<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:** [25 April 2023 09:07 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/6 "2023-04-25T09:07:58Z")

</div>

So, evens-stevens then? I have to say that I can never be bothered with the faff of messing around with the selection string and often end up wanting more of the variable anyway so I always just grab the thing and get on with the job. Not very often where a tiny saving would help anyway.

---

<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 June 2023 09:08 UTC](https://discourse.nodered.org/t/global-set-data-without-creating-an-array/77899/7 "2023-06-24T09:08:20Z")

</div>

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