0

I wonder how it is possible in a similar situation, the solution

var lang_object = {
    "UK": {
        "ERROR": {
            "fullname_empty": "fullname error",
            "phone_empty": "phone error",
        } 
    }
};

I have JSON Object.

var z = 'UK';
console.log(lang_object.z.ERROR.fullname_empty);

this example do not work, why? z = "UK".

var z = eval('UK');
console.log(lang_object.z.ERROR.fullname_empty);

this also dont work.

console.log(lang_object.UK.ERROR.fullname_empty);

this example work

Hang Prond
  • 59
  • 7

1 Answers1

1

Short answer: lang_object[z].ERROR.fullname_empty

Long answer:

object.z refers to the value of the key z in the object object.

object[z] refers to the value of a key equals to the value z, in the object object.

Gershon Papi
  • 5,008
  • 3
  • 24
  • 50