adding to json property that may or may not exist yet

前端 未结 8 2164
清酒与你
清酒与你 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:47

    var foo = dashboard['pages'] || {};
    foo['pagename'] = 'your thing here';
    dashboard['pages'] = foo;
    

    Explaining: the first line will check if the first value is null, undefined or... false (don't think this is a problem here): if it is, create a new object, if it is not, will use it.

提交回复
热议问题