Skip to main content

There isn't a built-in way for this kind of check, but you can implement it easily. Create a function, pass a string representing the property path, split the path by ., and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath){ if(!propertyPath) return false; var properties = propertyPath.split('.'); var obj = this; for (var i = 0; i < properties.length; i++) { var prop = properties[i]; if(!obj || !obj.hasOwnProperty(prop)){ return false; } else { obj = obj[prop]; } } return true; }; // Usage: var obj = { innerObject:{ deepObject:{ value:'Here am I' } } } obj.hasOwnNestedProperty('innerObject.deepObject.value'); 

Object.prototype.hasOwnNestedProperty = function(propertyPath) { if (!propertyPath) return false; var properties = propertyPath.split('.'); var obj = this; for (var i = 0; i < properties.length; i++) { var prop = properties[i]; if (!obj || !obj.hasOwnProperty(prop)) { return false; } else { obj = obj[prop]; } } return true; }; // Usage: var obj = { innerObject: { deepObject: { value: 'Here am I' } } } console.log(obj.hasOwnNestedProperty('innerObject.deepObject.value'));

There isn't a built-in way for this kind of check, but you can implement it easily. Create a function, pass a string representing the property path, split the path by ., and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath){ if(!propertyPath) return false; var properties = propertyPath.split('.'); var obj = this; for (var i = 0; i < properties.length; i++) { var prop = properties[i]; if(!obj || !obj.hasOwnProperty(prop)){ return false; } else { obj = obj[prop]; } } return true; }; // Usage: var obj = { innerObject:{ deepObject:{ value:'Here am I' } } } obj.hasOwnNestedProperty('innerObject.deepObject.value'); 

There isn't a built-in way for this kind of check, but you can implement it easily. Create a function, pass a string representing the property path, split the path by ., and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath) { if (!propertyPath) return false; var properties = propertyPath.split('.'); var obj = this; for (var i = 0; i < properties.length; i++) { var prop = properties[i]; if (!obj || !obj.hasOwnProperty(prop)) { return false; } else { obj = obj[prop]; } } return true; }; // Usage: var obj = { innerObject: { deepObject: { value: 'Here am I' } } } console.log(obj.hasOwnNestedProperty('innerObject.deepObject.value'));

Active reading.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

There isn't a built-in way for this kind of check, but you can implement it easily. Create a function, pass a string representing the property path, split the path by ., and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath){ if(!propertyPath) return false;   var properties = propertyPath.split('.'); var obj = this;   for (var i = 0; i < properties.length; i++) { var prop = properties[i];   if(!obj || !obj.hasOwnProperty(prop)){ return false; } else { obj = obj[prop]; } } return true; }; // Usage:  var obj = { innerObject:{ deepObject:{ value:'Here am I' } } } obj.hasOwnNestedProperty('innerObject.deepObject.value'); 

There isn't a built-in way for this kind of check but you can implement it easily. Create a function, pass a string representing the property path, split the path by . and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath){ if(!propertyPath) return false;   var properties = propertyPath.split('.'); var obj = this;   for (var i = 0; i < properties.length; i++) { var prop = properties[i];   if(!obj || !obj.hasOwnProperty(prop)){ return false; } else { obj = obj[prop]; } } return true; }; // Usage:  var obj = { innerObject:{ deepObject:{ value:'Here am I' } } } obj.hasOwnNestedProperty('innerObject.deepObject.value'); 

There isn't a built-in way for this kind of check, but you can implement it easily. Create a function, pass a string representing the property path, split the path by ., and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath){ if(!propertyPath) return false; var properties = propertyPath.split('.'); var obj = this; for (var i = 0; i < properties.length; i++) { var prop = properties[i]; if(!obj || !obj.hasOwnProperty(prop)){ return false; } else { obj = obj[prop]; } } return true; }; // Usage: var obj = { innerObject:{ deepObject:{ value:'Here am I' } } } obj.hasOwnNestedProperty('innerObject.deepObject.value'); 
Source Link
Viktor Bahtev
  • 4.9k
  • 2
  • 36
  • 42

There isn't a built-in way for this kind of check but you can implement it easily. Create a function, pass a string representing the property path, split the path by . and iterate over this path:

Object.prototype.hasOwnNestedProperty = function(propertyPath){ if(!propertyPath) return false; var properties = propertyPath.split('.'); var obj = this; for (var i = 0; i < properties.length; i++) { var prop = properties[i]; if(!obj || !obj.hasOwnProperty(prop)){ return false; } else { obj = obj[prop]; } } return true; }; // Usage: var obj = { innerObject:{ deepObject:{ value:'Here am I' } } } obj.hasOwnNestedProperty('innerObject.deepObject.value');