Skip to main content
Oct 12, 2017 7:15 PM UTC
Answered

How to Reference a Variable with a String

I have used a series of calculations and a concatenation to build a string.  I want to use this string to then retrieve the value of a variable that has already been set.

Here's an example:

function endValue();

var returnVariable  //variable for function to return - should have a value of "The Value I want in the End" when returned

var example1 = "already";

var example2 = "Set";

var alreadySet = "The Value I want in the End";

var concatenatedString = example1 + example2;//would have a value of "alreadySet"

// here's where I'm lost. This sets the value of returnVariable to "alreadySet" instead of "The Value I want in the End"

returnVariable = concatenatedString;

return returnVariable;

}

I hope I've explained this well enough.  I've tried searching for help, but I'm not even sure what I'm looking for.

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
Oct 16, 2017 2:15 PM UTC

Same question though - how could I use arrays and call the value based on a concatenated string? 

For each item, the color, size, shape, and quantity are ALL used to determine the price.  So each item has a price code something like ColorSizeShapeQty - which I used as a variable name for the price.

What I do is I first run some code to get the color code, then to get the size, then the shape, then the quantity and I concatenate them all together to go get the value of the price of that item.  I'm not sure how I could structure arrays to do this.  (PS - I realize a more experienced programmer might make easy work of this, but it's beyond my scope of programming knowledge.)


You can use an associative array:

JavaScript Arrays

11 replies

try67
Community Expert
Community Expert
Oct 12, 2017 7:57 PM UTC

Your variables names and values are quite confusing. Also, your function definition is incorrect. You need to replace the semi-colon in the first line with an opening curly bracket.

I don't really follow what you're trying to do here. In line #7 you set the value of concatenatedString to be "alreadySet". Why are you surprised that that's the assigned to returnVariable in line #8, then?

Translate
Oct 12, 2017 9:31 PM UTC

Thank you for taking the time to look at it.  I just tried to write a paraphrased code to show you what I'm trying to do.  The semicolon was a mistake on the paraphrase - I have a bracket in my real code. 

I'm not surprised that the value of concatenatedString is "alreadySet" - that's correct - it's the name of the variable I want to actually get the value of.  Now how do I get the value of the variable alreadySet using that string?

If you need more detail to understand where I'm going, I have an extensive list of price codes. Each price code is a variable and the price is the value.  I can take details about the products (size, color, etc) and systematically determine the price code for an item using the concatenation. Then, I need to use that price code in order to set the price.

Translate
try67
Community Expert
Community Expert
Oct 12, 2017 10:00 PM UTC

That's a very roundabout way of doing things, and not recommended. It is possible, though, using the eval method, which evaluates code on-the-fly. But I would highly recommend you do it differently, for example using literal objects which can have a name that you can access, as well as a set of properties.

Translate