1

I am trying to dynamically assign data to a variable based on object array values. I am going to present a simplified data structure:

const data = [{"age":25,"name":"Michael"},{"age":20,"name":"Lara"}]
const data2 = [{"age":26,"name":"Sarah"},{"age":21,"name":"David"}]

I want to assign values to variables. For example, I know this works:

const arr = 'data'
const zero = '0'
const a = 'age'
const test = `${data[zero][a]}`
console.log(test) // returns 25

But can I assign it dynamically(maybe nested template literal)?

const test = `${arr[zero][a]}`
console.log(test) // does not work, because arr is a string

const test = `${${arr}[zero][a]}`
console.log(test) // does not work

Is there any way to achieve this? Thank you in advance!

Edit:
I overthought the solution(tried to be fancy with template literals).
To solve it, thanks to the input from ggorlen I changed the way my data was stored. That way, it was way easier to access the data dynamically. The solution was fairly similar to the answer from Sowmen Rahman.

I was trying too hard to think about solving a problem in a specific way, when a different approach was more sensible!

  • 1
    Welcome to SO! If you're trying to treat the string `arr = 'data'` as a variable name, you could use `this[arr]` but using string data as variable names [is generally not a good pattern](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript). What are you [really trying to accomplish](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) with these machinations? Thanks for clarifying. – ggorlen Jun 29 '21 at 20:35
  • I am looping over the data arrays, and I need to extract the values from the arrays based on variables. Then assign those values to a new variable. I'll check that link, thank you! – Branislav Kockica Jun 29 '21 at 20:41
  • 1
    I see, thanks for that. The general solution then is to use a 2d array. Instead of `data`, `data1`, `data2`, ... `data4532`, `data4533` as separate loose variables (essentially keys on `this`), use `data = [[{"age":25,"name":"Michael"},{"age":20,"name":"Lara"}], [{"age":26,"name":"Sarah"},{"age":21,"name":"David"}]]`. Then you can loop over `data` and use `data[i][j]` to access an element `j` on the `i`th inner array. How are you getting your separate `data` variables in the first place? Keep things packed up into data structures to the extent you can so you can manipulate them easily. – ggorlen Jun 29 '21 at 20:43
  • 1
    Thanks for your help. You were right, and I changed how my data was stored and that solved it. I was going for something overcomplicated when there was no need for it. – Branislav Kockica Jun 30 '21 at 16:22
  • 1
    Great to hear. You might want to post a [self answer](https://stackoverflow.com/help/self-answer) describing what the problem was and how you fixed it, then accept it, to help future visitors with the same situation as you. The currently accepted answer doesn't really get to the heart of the problem. – ggorlen Jun 30 '21 at 16:42

1 Answers1

1

Something like this won't be possible in the way you're suggesting. Strings can't be dynamically converted to variable names by itself, however if you stored your variables in a dictionary or map object, you can use the subscript operator [] to achieve the task.

const data = [{"age":25,"name":"Michael"},{"age":20,"name":"Lara"}]
const data2 = [{"age":26,"name":"Sarah"},{"age":21,"name":"David"}]

const rootObject = {
  data: data,
  data2: data2
}

const arr = 'data'
const zero = '0'
const a = 'age'
const test = `${rootObject[arr][zero][a]}` 
console.log(test) // outputs 25
Sowmen Rahman
  • 344
  • 2
  • 5