1

I have a configuration json file in my application.

{
 "Name":{
     "Catgory":{
        "Val":{
           "Gaby": "122"
         },
        "Batch":{
            "Hede" : "86"
         }
       }
    }
 }

I have lot of configurations like the above. But when I try to read that like below, I'm unable to do.

I have imported the file as config in the current file. When I try to read as below I'm able to get the values.

config.Name.Catgory.Val 

But in place of Val,if I try to get the value dynamically like

configVal = "Val"

And if I try to call,

config.Name.Catgory.configVal I'm unable to read. Can anyone please help me with this?
sai
  • 487
  • 1
  • 7
  • 16
  • Duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Guy Incognito Oct 18 '20 at 07:50

1 Answers1

0

You need to use the bracket-notation in order to access values dynamically:

const config = {
 "Name":{
     "Catgory":{
        "Val":{
           "Gaby": "122"
         },
        "Batch":{
            "Hede" : "86"
         }
       }
    }
 }
 
 let configVal = 'Val';
 console.log(config.Name.Catgory[configVal]);
eol
  • 23,236
  • 5
  • 46
  • 64