1

So I've been needing something like this for a bit now but never been able to find something that suits my needs, Most likely I'm searching the wrong things!

My goal is to put a parameter from a function like foobar("test") and put the parameter plus some pre-written strings from the function and plus them together and therefore becoming a pre-existing variable...

I think I'm explaining very poorly... but hopefully, the code example gets a better explanation...

I've tried a few simple things that were obvious won't work so I most likely don't need to show what I did...

var text1 = "this is text"

function foo(id) {
    object.innerHTML = "text" + 1 // Then plus those together and make it set it to the variable text1
}

foo(1)

Expected: I would want it to write in the object "this is text" because it makes text and the id variable aka a 1 into text1 which is a variable with the string of "this is text"

Actual Results: Obviously that won't work I don't even need to write it to be able to find out that this won't work... Just an example to better explain...

Hopefully you can help, thx!

undefinedChar
  • 537
  • 1
  • 5
  • 18
  • Possible duplicate of [Use dynamic variable names in JavaScript](https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript) – George Jan 04 '19 at 09:53
  • As per my understanding you want to give a number to parameter and then generate a variable name in that function and then get the value of that variable ? – Ans Bilal Jan 04 '19 at 09:53
  • 2
    Doing this is bad. Use an array instead: `var texts = ["bla", "bli"];` then use `element.innerHTML = text[id];` (the answers you're getting will work, but it's still unnecessary and bad practice to do this) –  Jan 04 '19 at 09:55
  • @ChrisG This is not bad at all, this is how a dictionary is done in JavaScript, the only bad thing about the example code is that all the variables are on the global object. Using an array the way you have suggested would be worse as you'd need to make sure that the `id` is the same as the position of the object in the array. – George Jan 04 '19 at 10:07
  • @George A dictionary? Do you mean an Object or a Map? Anyway, [using eval is bad](https://stackoverflow.com/a/87260/5734311), and so is composing keys, and I don't understand what you mean by `you'd need to make sure that the id is the same as the position of the object in the array` This question is about reflection, and afaik it is a last resort and should be avoided. –  Jan 04 '19 at 10:14
  • @ChrisG I mean an Object as that is a dictionary. I've never mentioned using eval, I'm on about using the bracket notation. The way you have said in your comment (using an array) means if I had the `id` of 20 it'd have to be in the 21st position of the array. – George Jan 04 '19 at 10:24

3 Answers3

1
var text1 = "this is text"

function foo(id) {
    object.innerHTML = window['text'+id];}

foo(1)
Ans Bilal
  • 987
  • 1
  • 10
  • 28
0

You have to use eval function.

var text1 = "this is text"
var text2 = "this is text2"
function foo(id) {
   console.log(eval('text'+id)); 
}

foo(2)
0

As I explained in my comment, taking the question at face value leads to recommending something that is usually unnecessary and error prone, and should therefore be avoided.

There are much more readable alternatives, like creating an object and using its keys and [] notation:

var texts = {
  example: "this is text",
  message: "Hello World!"
};

function foo(id) {
  someDiv.innerHTML = texts[id];
}

foo("message");
<div id="someDiv"></div>