-1

I'm not able to get the value by dynamic key in following code,

const data = {
        "Id": "1234",
        "status": "open",
        "Translations": {
            "EN": "English",
            "ES": "Spanish",
            "FR": "French"
        }
    };
const translationKey = "FR";

console.log(data.Translations.translationKey)

How can I get "French" text from the object, If translationKey is dynamic ?

Any help is much appreciated.

Aamir
  • 2,173
  • 1
  • 29
  • 58

1 Answers1

3

Use bracket notation if you want to use a variable property name data.Translations[translationKey]. Here you will find more details.

It's the same notation one uses for accessing array elements, since an array is also an object.

kai
  • 6,702
  • 22
  • 38
  • Thanks, I assumed it can not be accessed like array..! – Aamir Jul 16 '20 at 11:34
  • In javascript an `Array` is also an `Object`. Therefore its the same notation to access an `Array` property at a specific index. – kai Jul 16 '20 at 11:35