0

I am defining an object inside a file I will export and require it has alot of config keys

var a = {
   main: { key1 : {label: 'one' value: 1 },
           key2 : {label: 'two' value: 2 },
          //etc...
          }
   selectedKeyValue : this.main.key1.label 
}

the benefit being that label can change often and I can always keep track of what it is if it ever gets edited, without ever needing to edit selectedKeyValue

This obviously give me and error, but is there anyway to have access to the obj's values while defining it such that I can do this?

jcubic
  • 61,973
  • 54
  • 229
  • 402
lonewarrior556
  • 3,917
  • 2
  • 26
  • 55

1 Answers1

1

You can not use a variable without defining it. But you can do something like this:

var a = {
   main: { key1 : {label: 'one' value: 1 },
           key2 : {label: 'two' value" 2 },
          //etc...
          }
};

a.selectedKeyValue = a.main.key1.label; 
void
  • 36,090
  • 8
  • 62
  • 107