我有一个这样的示例对象:
const test1 = {
a: {
b: {
c: 2
}
}
}
当我传递类似“ a”,“ a.b”或“ a.b.c”的参数时。如何有效地访问对象的正确级别?
我试图遍历它,但是找不到能正确执行它的方法。
有什么想法吗?谢谢,
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