0

I have an array of Objects: recs6, recs7, recs8...... recs23. I want to add a factor in the array using the inner for loop. But 'recs+n' is interpreted as string and not as variable name. How can I make it represent the existing variables - rec6, rec7 etc?

for(var n=6; n<24; n++){

    for(var m=0; m<'recs+n'.length; m++){
        hourBusyness += parseFloat(('recs'+n)[m].gtse);
    }

    hourAvgbusyness = hourBusyness / ('recs'+n).length;
    console.log(hourAvgbusyness);
}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Dan
  • 801
  • 2
  • 14
  • 29
  • 10
    Don't! Refactor to use a real array instead of a bunch of independent variables with similar names. We have arrays in programming languages for precisely that reason! – Quentin Jul 31 '13 at 09:31
  • @Quentin - I broke down a big json array by using switch case to make these hourly recs. So recs6 contains all those records which have a date in which hour is 6. Then i would aggregate the gtse and find an average for the 6th hour – Dan Jul 31 '13 at 09:56
  • Break it down into an array of arrays, not a bunch of variables (with similar names) containing arrays. – Quentin Jul 31 '13 at 09:58

6 Answers6

3

Use this['recs'+n] instead of ('recs'+n)

Pylinux
  • 11,278
  • 4
  • 60
  • 67
  • 1
    You're assuming they're globals. – T.J. Crowder Jul 31 '13 at 09:36
  • … and that the code doesn't need to work in strict mode. – Quentin Jul 31 '13 at 09:46
  • Yes this is a flawed solution. But this is the answer to the question. The best solution in this case is probably to use an array, if he is in controll of the creation of the variables. But this is not a question about code convention, but about using string as a variable. – Pylinux Jul 31 '13 at 09:56
0

"...as an array of Objects..." Without showing us that array, we have to guess at what you mean by that.

But if you put those on an object as properties:

var foo = {
    recs6: {/*...*/}, // Or are these arrays? See note below
    recs7: {/*...*/},
    recs8: {/*...*/},
    recs9: {/*...*/},
    // ...
};

Then you can use bracketed notation to access them:

for(var m=0; m < foo['recs'+n].length; m++){

Or alternately, as dystroy points out in a comment, just use an array:

var recs = [
    {/*...*/}, // Or are these arrays? See note below
    {/*...*/},
    {/*...*/},
    {/*...*/},
    // ...
];

for(var m=0; m < recs[n].length; m++){

Note that you're using the length property on them, which suggests that they're arrays, not just plain objects, but the fundamental thing above is how to get the thing (foo['recs'+n]).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

I'm not sure you can do that. You can, however, put your recs* in a dictionary, so that you can write: recsDict['recs'+n] to have access to your nth recs array.

axelcdv
  • 753
  • 6
  • 18
0

You can kind of cheat by using a JSON object.

var t = {};
t.recs6 = values;
//later...
t[('recs'+n)]
//can retrieve your data.
Grallen
  • 1,620
  • 1
  • 17
  • 16
0

As Quentin says, this looks like a code-smell to me. Could you try refactoring so that recs is an array of arrays?

However, the following works:

window['recs' + n].length
Phylogenesis
  • 7,775
  • 19
  • 27
0

You could do create an array where key are recsN, and access it like :

var array = [["recs1",5],["recs2",10],...,["recs23", 42]];

for (var i = 0; i < array.length;i++) {
    for(var m=0; m<array[i].length; m++){
        hourBusyness += parseFloat(array[i][m].gtse);
    }
Yabada
  • 1,728
  • 1
  • 15
  • 36