SHARE
TWEET

Untitled

a guest Oct 21st, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const deepContainsProperty = (data, testProperty) => {
  2.   const hasProperty = testObject => Object.keys(testObject).includes(testProperty);
  3.  
  4.   // check if top level object has the property itself before looping
  5.   if (hasProperty(data)) {
  6.     return true;
  7.   }
  8.  
  9.   let propertyFound = false;
  10.  
  11.   for (let property in data) {
  12.     const currentProperty = data[property];
  13.  
  14.     if (typeof currentProperty !== 'object' || Array.isArray(currentProperty)) {
  15.       continue;
  16.     }
  17.  
  18.      propertyFound = deepContainsProperty(currentProperty, testProperty);
  19.    
  20.     if (propertyFound) {
  21.       break;
  22.     }
  23.   }
  24.  
  25.   return propertyFound;
  26. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top