adding to json property that may or may not exist yet

前端 未结 8 2205
清酒与你
清酒与你 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()

提交回复
热议问题