如何使用字符串参数访问对象的更深层次?

我有一个这样的示例对象:

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

bbb06070241 回答:如何使用字符串参数访问对象的更深层次?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3122855.html

大家都在问