1

I'm trying to display content based on whether a property exists or not. The problem is that the direct parent is named depending on the link the user clicks. Example:

if (content.aPage.data){
//show some content
}
else {
//show other content
}

in the example above the "aPage" property could be named "bPage", "cPage" etc... some of these pages have the 'data' prop, some don't. It doesn't make sense for me to write a switch or if statements for all of the pages that don't have the 'data' prop because there are too many. Is there another way to do this?

xyzcode
  • 219
  • 1
  • 7
  • 14

1 Answers1

0

If you are aware of which page user is on. You can do following.

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
}
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19