-2

Let's say I have this obj here:

let obj1 ={
  a: 4,
  b: {
    ab: 44,
    c: {
      tres: 100
    }
  }
};

How can I access the tres property using a string? I can access b object using this code:

let prop = "b";
console.log(obj1[prop]); // returns b property

But how can I get access to a tres property that resides inside c inside b inside obj1 object?

Ruslan Plastun
  • 1,985
  • 3
  • 21
  • 48

1 Answers1

0

You can simply use dot (.) operator to chain them on the computed property ([prop]) name:

let obj1 ={
  a: 4,
  b: {
    ab: 44,
    c: {
      tres: 100
    }
  }
};


let prop = "b";
console.log(obj1[prop].c.tres);
Mamun
  • 66,969
  • 9
  • 47
  • 59