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

  • Home
  • SEARCH
  • 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 703827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:52:26+00:00 2026-05-14T03:52:26+00:00

let say I want to do this: var dashboard = {}; var page =

  • 0

let say I want to do this:

  var dashboard = {};
  var page = "index";

  $('.check').click(function(){ 
    $(this).toggleClass("active").siblings().slideToggle('slow', function() {
        dashboard['pages'][page][$(this).closest('li').attr("id")]['show'] = $(this).is(":hidden") ? 'collapsed' : 'expanded';
    });
  }

I get an error saying:

Dashboard.pages is undefined

Is there away to dynamically add pages and the children that follow without having to do the work of checking to see if it is defined first then if it’s not doing:

   dashboard['pages'] = {};

because sometimes they may already exist and I don’t want to have to inspect the tree first I just want to build the branches as needed

EDIT
I changed pagename to page to show that page names will change and also I want to point out that the pages could really be anything too.
The idea is that you have any object that can contain objects with parameters without checking to see if the branches exist

It looks like $extend as stated will be the way to go just not sure how that works. Got to get my head around that.

  • 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-14T03:52:27+00:00Added an answer on May 14, 2026 at 3:52 am

    Define get and set methods on an Object. Actually it could be defined just on the dashboard object and only its descendants, but that’s easy to do.

    Object.prototype.get = function(prop) {
        this[prop] = this[prop] || {};
        return this[prop];
    };
    
    Object.prototype.set = function(prop, value) {
        this[prop] = value;
    }
    

    Iterate through nested properties using this get() method and call set() whenever a value has to be set.

    var dashboard = {};
    
    dashboard.get('pages').get('user').set('settings', 'oh crap');
    // could also set settings directly without using set()
    dashboard.get('pages').get('user').settings = 'oh crap';
    
    console.log(dashboard); //​​​​​​​​​​​​​​​ {pages: {user: {settings: "oh crap"}}};
    

    You could also extend/modify the get method to accept the nested properties as individual arguments or an array or a string. Using that, you’d only have to call get once:

    // get accepts multiple arguments here
    dashboard.get('pages', 'user').set('settings', 'something');
    
    // get accepts an array here
    dashboard.get(['pages', 'user']).set('settings', 'something');
    
    // no reason why get can't also accept dotted parameters
    // note: you don't have to call set(), could directly add the property
    dashboard.get('pages.user').settings = 'something';
    

    Update:

    Since the get method generically returns an object and does not know whether you need an array or some other type of object, so you would have to specify that yourselves:

    dashboard.get('pages.user').settings = [];
    

    Then you could push items to the settings array as

    dashboard.get('pages.user').settings.push('something');
    dashboard.get('pages.user').settings.push('something else');
    

    To actually have the get function construct the object hierarchy from a given string such as pages.user, you would have to split the string into parts and check if each nested object exists. Here is a modified version of get that does just that:

    Object.prototype.get = function(prop) {
        var parts = prop.split('.');
        var obj = this;
        for(var i = 0; i < parts.length; i++) {
            var p = parts[i];
            if(obj[p] === undefined) {
                obj[p] = {};
            }
            obj = obj[p];
        }
        return obj;
    }
    
    // example use
    var user = dashboard.get('pages.user');
    user.settings = [];
    user.settings.push('something');
    user.settings.push('else');
    
    console.log(dashboard); // {pages: {user: {settings: ["something", "else"] }}}
    
    // can also add to settings directly
    dashboard.get('pages.user.settings').push('etc');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm at http://www.domain.com/page.html . I want to submit this form: <FORM METHOD=post
Let's say this page www.example.com/mypage returns some html that I want to parse in
For Actionscript 2.0 Let's say this page www.example.com/mypage returns some html that I want
let's say Expr = 1+2. and I want this Expr in parenthesis, what should
Let's say I want to do something like this void my_printf(char *fmt,...) { char
Let's say I want a method which will be called like this: tiger =
I've had this problem a couple of times. Let's say I want to display
Let's say you want to write a function which gets passed an opaque handle
Let's say I want to have a function which reads data from the SerialPort
Let's say I want to do this: $a = array_intersect_assoc( array( 'key1' => array(

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.