1

It might be a beginner's question, but I can't seem to find an answer on this.

The data it is getting is data out of a JSon file. I want it to loop through all the rows it is seeing. The loop works how it is written below and returns me the info I need with the rest of the code. I am trying to create multiple variables like testVar1, testVar2, testVar3, .... I don't know if it is possible to do it this way, or if I need to find another solution.

   var i = 0;
    for (var x in data) {
        var testVar1 = data[0][1]; // works
        var testVar[i] = data[0][1];    // doesn't
        i += 1;
    }

How can I make the testVar[i] work ?
What is the correct syntax for this?

Takit Isy
  • 9,688
  • 3
  • 23
  • 47
  • 1
    You can't create dynamic vars like that, you could create an object and set properties on that instead. – Keith Dec 11 '18 at 15:42
  • 4
    this seems like an XY question, what are you trying to achieve exactly? most of the times, you can use an array or object for that.. there are solutions, but most of the time insecure, or working only with global variables (through `window` object) – Kaddath Dec 11 '18 at 15:42
  • where you have var i, add var testVar = []. So you create the array outside your loop – DaveG Dec 11 '18 at 15:43
  • FYI looking at your code, it'd be better to use a [for loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement), not a [for in loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for...in_statement) – George Dec 11 '18 at 15:44
  • Your question is not clear. Can you please rephrase your question? – Hamed Dec 11 '18 at 15:45
  • Can't find the dupe, but OP is trying to have multiple variables and set it dynamically. OP is trying to set `testVar0`where the number is dynamic... – epascarello Dec 11 '18 at 15:45
  • i am using it to read an array out of a Json file, but i am not too familiar with Javascript ;) – Karel Beenhakker Dec 11 '18 at 15:46
  • 1
    @epascarello [This one](https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript)? – Ivar Dec 11 '18 at 15:47
  • Please show us a concise snippet of your JSON. – Mitya Dec 11 '18 at 15:53
  • @KarelBeenhakker Why do you want multiple variables? What do you want to do with them? Doesn't an array suffice? – Ivar Dec 11 '18 at 15:54
  • I am creating the Json with the php funtion json_encode(), the array is written like and array ( array (1,2,3), array (1,2,3)). – Karel Beenhakker Dec 11 '18 at 15:57
  • @KarelBeenhakker I meant a JavaScript array. We are missing the ultimate goal you are trying to achieve. – Ivar Dec 11 '18 at 16:00
  • 1
    I am getting the data for posting mulitple markers on the Google API. i want to call multiple listeners so when i click one, they wont all open. For this purpose i want to create them by using the [i]. when 1 is clicked, only one will open. but it is drifting from the subject. is it possible to define varriables like this? if this is a dumb way to do, i will look for another solution. ;) – Karel Beenhakker Dec 11 '18 at 16:01

3 Answers3

1

Your code misses the initialization of your array variable: var testVar = [];.



Anyway, you may want to create those variables in the window object :

for (var i = 0; i <= 2; i++) {
  name = 'var' + i;
  window[name] = "value: " + i;
}

console.log(var0);
console.log(var1);
console.log(var2);

That way you can keep using the "short" variable name.

halfer
  • 19,824
  • 17
  • 99
  • 186
Takit Isy
  • 9,688
  • 3
  • 23
  • 47
0

You can wrap all those variables in an object.

instead of:

var testVar1 = data[0][1];

Try:

var Wrapper = {};

//inside the for loop:
    Wrapper["testVar" + i] = data[0][i];
    ...and so on.

You'd access them as Wrapper.testVar1 or Wrapper["testVar" + 1].

Tilepaper
  • 78
  • 5
0

The problem you're having is pretty simple. You try to declare a variable as an array and in the same statement try to assign assign a value to a certain index. The reason this doesn't work is because the array needs to be defined explicitly first.

var testVar[i] = data[0][1];

Should be replaced with:

var testVar = []; // outside the loop
testVar[i] = data[0][1]; // inside the loop

Resulting in:

var i = 0,
    testVar = [],
    data = [
        ['foo', 'bar', 'baz'],
        ['kaas', 'is', 'baas']
    ];

for (var x in data) {
    var testVar1 = data[0][1];
    testVar[i] = data[0][1];
    i += 1;
}

console.log('testVar1', testVar1);
console.log('testVar', testVar);
console.log('testVar[0]', testVar[0]);
console.log('testVar[1]', testVar[1]);

If i isn't an integer you should use an object instead. This can be seen in the answer of Tilepaper, although I advise against the use variables starting with a capital letter since they suggest a constant or a class.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52