Global.set data without creating an array

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:

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

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

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

everything works fine, but when I use

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

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.

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.

Or use square bracket notation in the path string

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

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

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:

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

I can get what I want to get!

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.

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.

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