2

I am trying to combine a string and number to a dynamiclly generated variable. Currently tried it this way:

const ElementCount = 2;

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

ElementCount will later on be dynamic.

The result of the above function should look like this:

SampleVariable1 = "test"
SampleVariable2 = "test"

My code seems to be wrong - what do I have to change here? Solution can be native JS or jQuery as well.

Thanks a lot!

9
  • 2
    let in scope is unknown outside. Commented Feb 18, 2019 at 10:31
  • 1
    SampleVariable1 is element of array or independent variable? Commented Feb 18, 2019 at 10:32
  • @MaheerAli: it's independent Commented Feb 18, 2019 at 10:34
  • @NinaScholz: good point! How to make this available outside of this funtion? Commented Feb 18, 2019 at 10:35
  • 1
    JS doesn't allow you to dynamically determine the name of a variable in the way you appear to be trying. You have to use if statements or a switch, or most useful, an array or object to hold the different values. Commented Feb 18, 2019 at 10:36

3 Answers 3

5

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";
}
Sign up to request clarification or add additional context in comments.

7 Comments

I like the array solution most so far, even though I am not that familiar with the usage.. After the declaration via array, how can get access to e.g. "SampleVariable1", so that I can make usage of its value in progress?
use Variables["SampleVariable1"] to get the value
Your "array solution" isn't actually using an array, because you're using a string key rather than a numeric index. It's effectively the same as the "object solution" except Variables will have access to standard array properties/methods - which won't then work properly because you've not given it any elements at numerically-indexed positions. To truly use an array you have to assign to Variables[i] rather than Variables["SampleVariable" + i]
seems to me that the new variables are not usable outside my declaration function. "SampleVariable1 is not defined"
Is that for the "eval" solution? (Which I would emphatically not recommend, use an array or object instead.) Looks like @tony.ga made a mistake there, one solution would be to replace the let with var
|
4

There are few mistakes in your code

  1. You using comma , to separate the statements. You should use semicolon ;
  2. You are declaring SampleVariable inside the for loop so its not avaliable outside. Declare it outside the loop.
  3. You shouldn't use independent variable for this purpose as they just differ by 1. You should store them in array and use SampleVariable[number] to access them.
  4. You should initialize i = 0 otherwise the first element of SampleVariable will be undefined

const ElementCount = 2;
let SampleVariable = [];
for (let i = 0; i < ElementCount; i++) {
    SampleVariable[i] = "test";
}
console.log(SampleVariable);

Comments

1

This is my solution

const ElementCount = 2;

for(i = 1; i <= ElementCount; i++) {
    this['SampleVariable'+i] = "test";
}
SampleVariable1 // "test" (browser)
this.SampleVariable2 // "test"

1 Comment

This works, but I don't like the indirection of using this when you know it's referring to the global object. Just use window['SampleVariable'+i] if you really want to do it this way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.