Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement