Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:10:21+00:00 2026-05-26T21:10:21+00:00

I get some json data, and there I have such a structure: ‘modules’ :

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:10:21+00:00Added an answer on May 26, 2026 at 9:10 pm

    You could do it like this:

    var categories, sections;    
    if ((categories = modules[categoryName]) && (sections = categories[sectionName])) {
      concrete = sections[pageName] || [];
    }
    else {
      concrete = [];
    }
    

    You could also create a more general solution:

    function traverse(source) {
      var path = Array.prototype.slice.call(arguments, 1);
      var current = source;
      for (var i = 0, l = path.length; i < l; ++i) {
        current = current[path[i]];
        if (!current) break; 
      }
      return current;
    }
    

    Use it like this:

    concrete = traverse(modules, categoryName, sectionName, pageName) || [];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website where an ajax call will get some Json data from
I have some JSON data that I get from a server. In my JavaScript,
I have list of divs with some data in there: <div style=border: 1px solid
I have some json-code with has multiple objects in it, as such: These are
I have been using jquery ajax method to get the json data. I have
I am trying to get some errors returned in JSON format. So, I made
I am trying to get some XML data with LINQ, but running into a
I have a large amount of static/rarely changing data in JSON format. To improve
I am trying to retrieve data in a JSON object (which I have validated
I am trying to get some data off my DB and then trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.