Check if object member exists in nested object

后端 未结 8 985
花落未央
花落未央 2020-11-27 05:00

Is there a simpler way than using ___ in object to check the existence of each level of an object to check the existence of a single member?

More conci

8条回答
  •  萌比男神i
    2020-11-27 05:20

    Something like (warning: untested code ahead)

    var testProperty = function(obj, proplist) {
       for (var i=0; i < proplist.length; i++) {
          if (obj.hasOwnProperty(proplist[i])) {
             obj = obj[proplist[i]];
          } else {
            return false;
          }
       }
       return true;
    }
    

提交回复
热议问题