adding to json property that may or may not exist yet

前端 未结 8 2161
清酒与你
清酒与你 2021-01-04 04:12

let say I want to do this:

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

  $(\'.check\').click(function(){ 
    $(thi         


        
相关标签:
8条回答
  • 2021-01-04 04:52

    $.extend is the way to go

    function elem(a,b){
    var r={'pages':{'pagename':{}}};
    r.pages.pagename[a]={'show':b};
    return r;
    }
    data={};
    //inside the event handler or something:
    data = $.extend(true,data,elem($(this).closest('li').attr("id"),$(this).is(":hidden") ? 'collapsed' : 'expanded'));
    

    But honestly - this is a rather messy idea to store this information anyway. I bet that if You need that information later it could be done with a good selector or with jQuery.data()

    0 讨论(0)
  • 2021-01-04 04:55

    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 讨论(0)
提交回复
热议问题