-1

Is it possible that the merging of sub variables fetches the constructed variable?

That's my script:

var a_b_c = 5000;

console.log(a_b_c); // 5000

var el_a = 'a';
var el_b = 'b';
var el_c = 'c';

console.log(el_a + '_' + el_b + '_' + el_c); // logs a_b_c

... what I would like to have is though:

console.log(el_a+'_'+el_b+'_'+el_c); // 5000

Is this anyhow possible?

Here is a fiddle:

https://jsfiddle.net/2ca9skg7/

isherwood
  • 52,576
  • 15
  • 105
  • 143
Philipp M
  • 2,946
  • 3
  • 28
  • 75

1 Answers1

1

var a_b_c = 5000;

console.log(a_b_c);

var el_a = 'a';
var el_b = 'b';
var el_c = 'c';

console.log(window[el_a+'_'+el_b+'_'+el_c]);

https://jsfiddle.net/oa3gw450/

or if it's in a function, you'll probably have to use eval:

(() => {
  var a_b_c = 5000;

  console.log(a_b_c);

  var el_a = 'a';
  var el_b = 'b';
  var el_c = 'c';

  console.log(eval(el_a+'_'+el_b+'_'+el_c));
})()
dave
  • 57,127
  • 4
  • 69
  • 87