adding to json property that may or may not exist yet

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

let say I want to do this:

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

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


        
8条回答
  •  Happy的楠姐
    2021-01-04 04:49

    I would do it with the ternary operator:

    dashboard['pages'][page] = dashboard['pages'][page] ? dashboard['pages'][page] : {};
    

    That will do the trick no matter if it's set/null or whatever.

提交回复
热议问题