0

I have objects which e.g. look like

var myObject = 
{ 
  propertyA: 'valueA',
  propertyB: 'valueB',
  propertyC: {
       subProperty1: 'subvalue1',
       subProperty2: 'subvalue2'
  }
}

In some config variable I retrieve some string, it could look like:

 configString1 = 'propertyA';
 configString2 = 'propertyC.subProperty2';

It's easy to retrieve the values from my object by the first string as property:

 var value = myObject[configString1]; // returns valueA

But how could I achieve the same thing for configString2 as

 var value = myObject[configString2]; // not defined

obviously is not working.

Of course I could start to check wether there's a dot in my string, split it and with a couple of if-else statements I can make sure to get the right values.

But is there some easy way to achieve that in a generic way with Javascript/Angular?

LBA
  • 3,859
  • 2
  • 21
  • 60
  • Your json is missing a quote in valueB, and try this one **var value = myObject.configString2;** – Sachila Ranawaka Jun 24 '16 at 10:23
  • Thanks @t-j-crowder - you're right, I am sorry, tried to find the answer but wasn't using the correct wording, will keep the question for others who might find the right answer through it. – LBA Jun 24 '16 at 10:42

0 Answers0