0

I am trying to combine the text values of 2 variables then use that to get the value of a 3rd variable and use that in a calculation. I am hoping if I state the (group + type) correctly that it can be done. With this code:

var group = "flowers";
var type = "rose";

var flowersrose = 20;

var result = 0;

result = result + (group + type);

output = [{id: 123, result: result}];

I get:

id
123
result
0flowersrose

I want:

id
123
result
20
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
chrisboat
  • 1
  • 2

1 Answers1

1

var group = "flowers"; 
var type = "rose";

var flowersrose = 20;

result = group + type;
console.log(result);

output = [{id: 123, result: window[result]}];
console.log(output);
flowersrose
[
  {
    "id": 123,
    "result": 20
  }
]

More information about the window[result] part:

Get global variable dynamically by name string in JavaScript

0stone0
  • 34,288
  • 4
  • 39
  • 64