0

I have a sample object like this:

const test1 = {
  a: {
    b: {
      c: 2
    }
  }
}

When I pass a parameter like: 'a', 'a.b' or 'a.b.c'. How can access the right level of the object in an effective way?

I tried to loop through it but couldn't figure out a method to do it properly.

Any ideas? Thanks,

const test1 = {
  a: {
    b: {
      c: 2
    }
  }
}

const item1 = 'a.b'
const item2 = 'a.b.c'

console.log(test1[item1]) // should be { c: 2 }
console.log(test1[item2]) // should be 2
Dale Nguyen
  • 1,930
  • 3
  • 24
  • 37

0 Answers0