adding to json property that may or may not exist yet

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

    If you use underscore >= 1.9.0, you can use property and propertyOf:

    d={a: {b: "c"}}
    _.property(["a", "b", "c", "e"])(d)   // undefined
    _.property(["a", "b"])(d)             // "c"
    _.propertyOf(d)(["a", "b", "c", "e"]) // undefined
    _.propertyOf(d)(["a", "b"])           // "c"
    

提交回复
热议问题