0

If I have a multi-level json object like this:

{
    employee : {
        name : "Blah",
        company : {
            name : "Company"
        }
    }
}

I know I can access company name like employee.company.name, but is there a way to do this dynamically? Like I tried employee["company.name"] But that doesn't work. Basically if I have the string "company.name" I want to get that value in one call.

Trant
  • 3,461
  • 6
  • 35
  • 57
  • The easiest way is to use Lodash/Undescore/Ramda. With Lodash/Underscore it's `_.get(employee, "company.name")` – VLAZ Jul 15 '21 at 21:17

1 Answers1

0

You can do this by accessing each sub-object separately:

employee["company"]["name"]

var data = {
    employee : {
        name : "Blah",
        company : {
            name : "Company"
        }
    }
};

console.log(data["employee"]["company"]["name"]);
gunwin
  • 4,578
  • 5
  • 37
  • 59