0

It is simple to create a variable such as "a", but as soon as I want to create "a.b", I need to know that "a" exists first, I'm trying to create a function such as "function SetPathedVariable(DottedName, Value)".

In the console I have tried to check and create things like "['a.b']" "['a']['b']" (and many other variations), sometimes preceded by "window" as they are global variables, so far nothing I have tried works.

To make global variables easier to manage and recognise I'm creating ONE global variable and grouping variables as required, for example this is the sort of thing I currently have to do first:

var GlobalVarJs = {}; GlobalVarJs.BtnHelp = {}; 

I want to remove the above initialization and if the first global variable is "GlobalVarJs.BtnHelp.SomeVariable" then I want it set and after creating the two above levels by executing:

SetPathedVariable("GlobalVarJs.BtnHelp.SomeVariable", "SomeValue")" 

Any help appreciated, I don't know enough Javascript to work it out yet.

Using eval() it would have been easy but I've been avoiding that (probably for no good reason). People talk about injection but is that a problem for information that doesn't come from user input? Most people are very vague in their statements on the security issues.

2
  • Lodash solved it for you: lodash.com/docs/4.17.15#set "If a portion of path doesn't exist, it's created" Seems like it's exactly what you want. Commented Aug 28, 2024 at 23:04
  • @MisterJojo - what if you later want to add to a.b.c.g - as much as I like one liners, I don't think that will work Commented Aug 28, 2024 at 23:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.