16

How would I create dynamic variable names in NodeJS? Some examples say to store in the window variable, but I was assuming that is client-side Javascript. Correct me if I'm wrong.

2
  • All variables are dynamic, did you mean to ask how to create globals in node? Commented Sep 26, 2012 at 1:00
  • You mean, how do you declare a variable? var x = 'val'; or x = 'val'; You may need to learn some more basic javascript before getting started with Node or anything else. Commented Sep 26, 2012 at 1:03

4 Answers 4

28

Generally you would do something like:

var myVariables = {};
var variableName = 'foo';

myVariables[variableName] = 42;
myVariables.foo // = 42
Sign up to request clarification or add additional context in comments.

1 Comment

Wrapping variable names into a standard object. Yes, very smart and clever :)
9

In node.js there is the global context, which is the equivalent of the window context in client-side js. Declaring a variable outside of any closure/function/module as you would in plain Javascript will make it reside in the global context, that is, as a property of global.

I understand from your question that you want something akin to the following:

var something = 42;
var varname = "something";
console.log(window[varname]);

This in node.js would become:

var something = 42;
var varname = "something";
console.log(global[varname]);

3 Comments

I know this is what he was hinting at in the question, but putting things in the global namespace is frowned upon in node. You won't find many libraries that do this kind of thing.
@Bill correct, but I was under the impression OP was confused at the lack of the window context in node.js and would like to know an equivalent.
Perhaps using "runInThisContext" with the vm module in node would be slightly preferred? davidmclifton.com/2011/08/18/node-js-virtual-machine-vm-usage
2

Just don't know what a bad answer gets so many votes. It's quite easy answer but you make it complex.

var type = 'article';
this[type+'_count'] = 1000;  // in a function we use "this";
alert(article_count);

1 Comment

0

One possible solution may be:
Using REST parameter, one can create an array and add each dynamic variable (REST parameter item) as an object to that array.

// function for handling a dynamic list of variables using REST parameters
const dynamicVars = (...theArgs) => {
  let tempDynamicVars = [];

  // as long as there are arguments, a new object is added to the array dynamicVars, creating a dynamic object list of variables
  for (let args = 0; args < theArgs.length; args++){
    const vName = `v${args}`;
    tempDynamicVars = [...tempDynamicVars, {[vName]: theArgs[args]}]; //using spread operator
    // dynamicVars.push({[vName]: theArgs[args]}); // or using push - same output
  }
  return tempDynamicVars;
}

// short version from above
// const dynamicVars = (...theArgs) => theArgs.map((e, i) => ({[`v${i}`]: e}));

// checking
const first = dynamicVars("F", 321);
console.log("Dynamic variable array:", first);
console.log(` - ${first.length} dynamic variables`);
console.log(" - second variable in the list is:", first[1], "\n");

console.log(dynamicVars("x, y, z"));
console.log(dynamicVars(1, 2, 3));
console.log(dynamicVars("a", "b", "c", "d"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.