-1

Supposing I have a JSON object

let dictionary = require('../../es.json');
let obj = JSON.parse(dictionary);

and I have a string variable

let jsonPropertyPath = 'property1.property2.property3'

Now, I would like to get the value of the property property1.property2.property3 of an object obj. Is there any elegant way of doing this?

KoBe
  • 1

1 Answers1

-1
export const FindTranslation = (jsonPropertyPath: string): string => {
    let dictionary = require('../locale/' + testContext.locale + '.json') as JSON;
    return eval('dictionary.' + jsonPropertyPath);
};
KoBe
  • 1
  • 1
    Don't `eval`. `jsonPropertyPath.split('.').reduce((o, p) => o[p], dictionary)` does just fine and is safe. – deceze Apr 27 '22 at 07:13