-1
blah('A');

function blah(letter){
arrayA.push('something');
}

I want to push something to an array where the name of the array is 'array' plus a letter being passed to it.

I can console out 'arrayA' fine: console.log('array'+${letter})

But if I try to build the array name, the same logic doesn't work: array${letter}.push('something')

  • how do you read that array (later)? – Nina Scholz Jul 07 '18 at 18:12
  • 2
    What you are asking for isn't generally how Javascript is written or how problems are solved in Javascript (there are usually better ways of doing things). What I'd suggest is that you describe the overall problem you're trying to solve (not the specific solution direction you've pursued) and then we can likely offer you a better way to solve your problem. – jfriend00 Jul 07 '18 at 18:14
  • Your current question is pretty much an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where you've discussed issues with your solution and not described the actual problem. That prevents us from understanding the actual problem and offering you different and better ways to solve it. – jfriend00 Jul 07 '18 at 18:15
  • Possible duplicate of https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript – Andres Salgado Jul 07 '18 at 18:17
  • Likely an array or using property names on an object are the appropriate solution, but don't really know without describing the actual problem. Numbered variables simply aren't the right way of recording things in Javascript. – jfriend00 Jul 07 '18 at 18:19

2 Answers2

0

In the browser (where the global objects, functions, and variables become members of the window object) you can create and access dynamically named objects using the bracket notation. Were you looking for something like this?

function blah(letter){
    window['array' + letter] = [];
    window['array' + letter].push('something');
}

blah('A');

After this you can access and use the newly created array (arrayA) as usual.

arrayA.push('something else');

In node you can probably achieve this using global instead of window.

Timovski
  • 571
  • 2
  • 6
0

Try it and forget it (or replace the 3 occurrences of window with global for testing with node.js):

function test(name,value){
  if(!window["array"+name])
    window["array"+name]=[];
  window["array"+name].push(value);
}

try{console.log(arrayA);}catch(e){console.log("arrayA missing: "+e);}
test("A",10);
try{console.log(arrayA);}catch(e){console.log("arrayA missing: "+e);}
test("A",20);
try{console.log(arrayA);}catch(e){console.log("arrayA missing: "+e);}

window is the global scope in a browser, and generally you should not rely on global variables without a good reason. They lack context (that is why they are 'global'), making it hard to tell where they belong, what they are and where they come from. That is something what most programming paradigms advise against.

The thing also works with node.js, just it has global as global context, you can paste this snippet into https://www.tutorialspoint.com/execute_nodejs_online.php as a test, replace the 3 window-s, and it will work (you can of course wrap it into a proper module too, just that is more work). What is written above against the usage of global variables stays true for node.js too. Do not use the global context especially if you are developing modules.

However, instead of window, the syntax works with any object too, and that would be considered okay:

var obj={};
console.log(obj.something);
obj['some'+'thing']=10;
console.log(obj.something);

So you can freely have your own 'context' object (if you write the var obj={}; line in the top-level of a module, it will be available everywhere in that module, and it will not interfere with the outside world), and create/access its members using this array-like syntax (obj['something']), constructing the names on the fly when necessary.

tevemadar
  • 12,389
  • 3
  • 21
  • 49