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

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
  • 4,718
  • 3
  • 20
  • 46