2

I can't figure out how to use dynamic variable in JS.. I have a list of ticker, some balances associated, and I want to display each 'balance sticker' in my loop, dynamically. But it seems like it's not using the append I add to the variable at all?

var ticker = [CURRENCY1,CURRENCY2,CURRENCY3];
var balancesCURRENCY1 = 20;
var balancesCURRENCY2 = 30;
var balancesCURRENCY3 = 40;

for (var tick in ticker) {

  if (('balances'+ticker[tick]) != 0) {
    console.log(true);
  }     

}
2
  • 1
    Use an array instead. (You could use eval, but you really, really shouldn't. Dynamic variable names are quite a bad code smell.) Commented Aug 20, 2018 at 4:01
  • 3
    ugh - redesign, reformat and refactor - nothing good will ever come of any answer to this question Commented Aug 20, 2018 at 4:13

3 Answers 3

2

To use dynmamic variables, keep the variableNames in an Object, whenever you create the variable name dynamically, refer the object to get the variable's value.

var ticker = ['CURRENCY1','CURRENCY2','CURRENCY3'];
 
var allBalances = {
  balancesCURRENCY1 : 20,
  balancesCURRENCY2 : 30,
  balancesCURRENCY3 : 40

}

for (var tick in ticker) {

  if (allBalances['balances'+ticker[tick]] != 0) {
    console.log(true);
  }     

}

Sign up to request clarification or add additional context in comments.

Comments

1

First of all you have to quote names in the array: [CURRENCY1,CURRENCY2,CURRENCY3], otherwise they would be handled as variables.

Then you can access your dynamic names from this see the snippet below:

var ticker = ['CURRENCY1', 'CURRENCY2', 'CURRENCY3'];
var balancesCURRENCY1 = 20;
var balancesCURRENCY2 = 30;
var balancesCURRENCY3 = 40;

for (var tick in ticker) {

  if (this['balances'+ticker[tick]] != 0) {
    console.log(
      ticker[tick], 
      'balances'+ticker[tick] + ' = ' + this['balances'+ticker[tick]],
      true
    );
  }     

}

4 Comments

What if I want to show the balancesCURRENCY1 value? I tried that but its returning "balancesCURRENCY1" and not the value.. var ticker = ['CURRENCY1', 'CURRENCY2', 'CURRENCY3']; var balancesCURRENCY1 = 20; var balancesCURRENCY2 = 30; var balancesCURRENCY3 = 40; for (var tick in ticker) { if (this['balances'+ticker[tick]] != 0) { console.log('balances'+ticker[tick], true); } }
@Vincent, this['balances'+ticker[tick]] returns the value.
I meant in the console.log(), im trying to get the value of each, but it says undefined even with this['balances'+ticker[tick]]
Thanks man! For some reason, this['balances'+ticker[tick]] is working here on stack overflow, but if I try it on other compiler its not. Do you know why is that? It says undefined.
0

It would be cleaner to maintain 2 parallel arrays

var ticker = ['CURRENCY1','CURRENCY2','CURRENCY3'];
var balances = [20, 30, 40];

or an array of objects:

var tickerInfo = [
  {currency: 'CURRENCY1', balance: 20},
  {currency: 'CURRENCY2', balance: 30},
  {currency: 'CURRENCY3', balance: 40},
];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.