我也说不清问题,只能举个栗子:
// 一个数组
const tmp = [
{
parent: {
childern: 1
}
},
{
other: {
childern: 2
}
},
{
parent: {
other: 3
}
}
]
for (let i = 0, len = tmp.length; i < len; i++) {
// 如果嵌套层级多,那么就要写一长串,用if的话要嵌套非常多层
typeof(tmp[i].parent) ? typeof(tmp[i].parent.childern) ? : console.log(true) : console.log(false) : console.log(false)
// JS有没有什么函数或者方法可以这样优雅的判断多层级对象呢?
if (优雅的函数(tmp[i].parent.childern)) {
console.log(true)
}
}
我遇到的数据嵌套层级已经超过5层,目前还是学生,奈何我菜所以来求教一下大佬这种情况如何处理呢?万分感谢!
tmp[i]?.parent?.childern