4

I'm writing code to set up a lookup structure in Javascript

var g_Codes = {
    "Type1": {
        "U.S.": "US1",
        "Asia": "AS1",
        "Europe": "EU1"
    },
    "Type2": {
        "U.S.": "X2",
        "Asia": "X2",
        "Europe": "X2"
    },
    "Type3": {
        "U.S.": "X3",
        "Asia": "X3",
        "Europe": "X3"
    },
    "Type4": {
        //  Does not exist yet
    }
};

So you can loop over this and get the code out by cross-referencing type & region.

However, I'd like to make it so I can do this:

var US_REGION = "U.S.";
var AS_REGION = "Asia";
var EU_REGION = "Europe";

var g_Codes = {
    "Type1": {
        US_REGION: "US1",
        AS_REGION: "AS1",
        EU_REGION: "EU1"
    },
    "Type2": {
        US_REGION: "X2",
        AS_REGION: "X2",
        EU_REGION: "X2"
    },
    "Type3": {
        US_REGION: "X3",
        AS_REGION: "X3",
        EU_REGION: "X3"
    },
    "Type4": {
        //  Does not exist yet
    }
};

and make it easier to change the standard region names.

If I try this:

for(let focus in g_Codes) {
     print(focus);
}

The first gives me "U.S.", "Asia", etc, while the second gives "US_REGION", "AS_REGION"

How can I use the variables, but keep the simplicity of looping over g_Codes?

2
  • You might want to reconsider using let, only FireFox supports it: en.wikipedia.org/wiki/JavaScript#Versions Commented Jul 28, 2011 at 13:29
  • Actually, I am, but I wanted to use a syntax that was more widely used for the question. Commented Jul 28, 2011 at 14:28

3 Answers 3

4

You can't, in the notation you want.

You can define the object first:

var g_Codes = {
    Type1: {},
    Type2: {},
    Type3: {}
};

And then populate it using variables for property names and square bracket notation:

g_Codes.Type1[US_REGION] = 'US1';
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's what I suspected. Thanks!
0

Why not this structure:

var g_Codes = {
    'Type1': ['US1', 'AS1', 'EU1'],
    'Type2': ['X21', 'X22', 'X23'],
    'Type3': ['X31', 'X32', 'X33']
};

Now you can use indexes to access the strings:

g_Codes.Type1[0] // returns 'US1'
g_Codes.Type2[1] // returns 'X22'

etc.

All you have to remember is which index (integer) represents which region.

You could define a helper object:

var REGIONS = {
    'US': 0,
    'Asia': 1,
    'Europe': 2
}

And then:

g_Codes.Type1[ REGIONS.Europe ] // returns 'EU1'

Comments

0

Can you re-organise your structure instead?

var US_REGION = "U.S.";
var AS_REGION = "Asia";
var EU_REGION = "Europe";

var g_Codes = {
    "U.S." : {
        "Type1" : "US1",
        "Type2" : "X2",
        "Type3" : "X3"
    },
    "Asia" : {
        "Type1" : "US1",
        "Type2" : "X2",
        "Type3" : "X3"
    },
    "Europe" : {
        "Type1" : "US1",
        "Type2" : "X2",
        "Type3" : "X3"
    }
};

var single_region = g_Codes['U.S.'];

for(var key in single_region) {
    console.log(key, single_region[key]);
}

// or a different loop
for (var region in g_Codes) {
    for(var key in g_Codes[region]) {
       console.log(key, g_Codes[region][key]);
    }
}

1 Comment

Thanks for trying, but that just gives me the same problem with "Type"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.