2

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!

5
  • 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. What are you really trying to accomplish with these machinations? Thanks for clarifying. Commented Jun 29, 2021 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! Commented Jun 29, 2021 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 ith 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. Commented Jun 29, 2021 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. Commented Jun 30, 2021 at 16:22
  • 1
    Great to hear. You might want to post a 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. Commented Jun 30, 2021 at 16:42

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.