Create Dynamic Variable Name

How do I create dynamic variable names inside a loop?

Use an array for this.

var markers = [];
for (var i = 0; i < coords.length; ++i) {
markers[i] = "some stuff";
}

Create dynamic variable name

C# is strongly typed so you can't create variables dynamically. You could use an array but a better C# way would be to use a Dictionary as follows. More on C# dictionaries here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickTest
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> names = new Dictionary<string,int>();


for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}

var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
}
}
}

Use dynamic variable names in JavaScript

Since ECMA-/Javascript is all about Objects and Contexts (which, are also somekind of Object), every variable is stored in a such called Variable- (or in case of a Function, Activation Object).

So if you create variables like this:

var a = 1,
b = 2,
c = 3;

In the Global scope (= NO function context), you implicitly write those variables into the Global object (= window in a browser).

Those can get accessed by using the "dot" or "bracket" notation:

var name = window.a;

or

var name = window['a'];

This only works for the global object in this particular instance, because the Variable Object of the Global Object is the window object itself. Within the Context of a function, you don't have direct access to the Activation Object. For instance:

function foobar() {
this.a = 1;
this.b = 2;

var name = window['a']; // === undefined
console.log(name);
name = this['a']; // === 1
console.log(name);
}

new foobar();

Javascript + build a dynamic variable name from a variable name and string

Here we build string and address window[yourvar] to increment, does this work for you

var player1InitStart = 0;var player2InitStart = 0;var player3InitStart = 0;var player4InitStart = 0;
//var playerID = event.target.id; // will return, player1, 2, 3, 4
document.querySelectorAll('div').forEach(div => { div.addEventListener('click', clickEvent);});
function clickEvent(event) { let playerID = `${event.target.id}InitStart`; window[playerID] += 1; logVars();}
function logVars() { console.log(player1InitStart); console.log(player2InitStart); console.log(player3InitStart); console.log(player4InitStart);}
<div id="player1">1</div><div id="player2">2</div><div id="player3">3</div><div id="player4">4</div>

How can you dynamically create variables?

Unless there is an overwhelming need to create a mess of variable names, I would just use a dictionary, where you can dynamically create the key names and associate a value to each.

a = {}
k = 0
while k < 10:
# dynamically create key
key = ...
# calculate value
value = ...
a[key] = value
k += 1

There are also some interesting data structures in the collections module that might be applicable.

Robotframework, How to define a dynamic variable name

This is currently not possible with Robot Framework. You can use "variables inside variables" to resolve the values of variables (see the documentation on this topic) but not to resolve/set the name of the variable itself.

Though I am afraid that would be confusing anyway. Maybe you can explain your motivations and people can come up with another solution for your problem.

Create dynamic variable names based on count result

solution is to use eval, but It's nasty code, Avoid using 'eval', just use an array or object.

1, eval solution:

const ElementCount = 2;

for (let i = 1; i <= ElementCount; i++) {
eval("let SampleVariable[" + i + "] = 'test'");
}

2, array solution:

const ElementCount = 2;
let Variables = []
for (let i = 1; i <= ElementCount; i++) {
Variables["SampleVariable" + i] = "test";
}

3, object solution:

const ElementCount = 2;
let Variables = {}
for (let i = 1; i <= ElementCount; i++) {
Variables["SampleVariable" + i] = "test";
}

JavaScript Dynamic Variable Names

You can only do that with bracket notation, which means you have to attach the variables to something.

The global scope would be window, so that would be window['hello' + newCount], but polluting the global namespace with a bunch of random properties doesn't sound like a good idea, so using an object seems better

var vars = {};
var newCount = parseInt($('#hello').html(), 10);

$('.hello').click(function(){
newCount++;
vars['hello' + newCount] = '<p>Hello World</p>';
});

alert( vars['hello1'] );

FIDDLE



Related Topics



Leave a reply



Submit