I get some json data, and there I have such a structure:
'modules' : {
'category1' : {
'section1' : {
'page1' : [ 'module1', 'module2' ]
},
'section2' : {
'page1' : [ 'module1' ]
}
},
// and so on
}
I need to get all modules of some page.
Input parameters are category, section and page.
So i need to get modules[category][section][page].
But what if there are no modules for such a section, or page.
// m = getAllModules();
if ( m.hasOwnProperty(category) ) {
if ( m[category].hasOwnProperty(section) ) {
if (m[category][section].hasOwnProperty(page)) {
concrete = m[category][section][page];
}
}
}
How to optimize this script? It’s lot of calls of m[]. Is there a better(faster) way to do this?
You could do it like this:
You could also create a more general solution:
Use it like this: