我正在尝试根据属性是否存在来显示内容。问题在于直接父级的命名取决于用户单击的链接。示例:
if (content.aPage.data){
//show some content
}
else {
//show other content
}
在上面的示例中,“ aPage”属性可以命名为“ bPage”,“ cPage”等。这些页面中有些具有“数据”属性,有些则没有。对我来说,写一个开关或所有不具有“数据”属性的页面的语句都不是合理的,因为太多了。还有另一种方法吗?
答案 0 :(得分:0)
如果您知道哪个页面用户打开。您可以执行以下操作。
const page = 'aPage' // this will change as per page
if (content[page] && typeof content[page] === 'object' && content[page].data){
//show some content
}
else {
//show other content
}