0

I am using for loop to declare some objects, and it looks like below

var variables = {};

for (var i = 0; i < 3; i++) {

  variables['data' + i] = i;

}

And after i used these method i can use these variable like below

   console.log(variables); //call all
   console.log(variables.data1); //call Single

But i am wondering if i am able to call all in another way like

  var variables = {};

    for (var i = 0; i < 3; i++) {

      variables['data' + i] = i;

      //maybe i could export all data this way?
      var result = variables.data+i 

    }

I am doing this becuase i have speical usage of

var result = [variables.data0,variables.data1,variables.data2]

i want to use a loop to do this.

Anson Aştepta
  • 1,125
  • 2
  • 14
  • 38
  • You can't use dot notation like that, use bracket notation: `var result = variables['data'+i]`. – RobG Aug 18 '16 at 04:15
  • *Before the loop:* `var result = [];` and then *in* the loop `result.push(variables['data'+i]);` (Sorry - I may have been a bit hasty in closing this as a duplicate. The linked question covers the access of property names using a variable, but not the part about adding to the array.) – nnnnnn Aug 18 '16 at 04:18
  • console.log(seriesbb['data'+i]) returns undefine – Anson Aştepta Aug 18 '16 at 04:19
  • `seriesbb` isn't shown in your question at all. – nnnnnn Aug 18 '16 at 04:19
  • OPPS SORRY , i mean (variables['data'+i]), but i solve it now, according your method @nnnnnn – Anson Aştepta Aug 18 '16 at 04:34

0 Answers0