0

I can't access the variable with a current name that has an index indicator dynamically

let arr = []
let var0 = {"key1": "a"}
let var1 = {"key1": "b"}

for (let i = 0; i < 2; i++) {
  arr.push(var[i].key1)
}

My goal is to end with an array like this arr = ["a", "b"]

  • Why not use an array for `var0` and `var1`? – kelsny Mar 21 '23 at 15:36
  • 1
    keep an object. `const obj = {var0:{key1:'a'},var1:{key1:'b'}}` . then you can access like `obj['var'+i].key1` – cmgchess Mar 21 '23 at 15:36
  • Does this help? https://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name – evolutionxbox Mar 21 '23 at 15:37
  • Because actually I'm testing a component that can make many requests... and for each request I'm storing the response within an variable. After that I need to access each variable to create another HTTP request using the response values. One for each different 'attribute' – FernandoQA Mar 21 '23 at 15:42

0 Answers0