Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. function search(obj, searchedValue, path = '') {
  2. if (Array.isArray(obj)) {
  3. obj = Object.assign({}, obj);
  4. }
  5.  
  6. const found = Object.entries(obj).find(([key, value]) => {
  7. if (value === searchedValue) {
  8. path += `/${key}`;
  9. return true;
  10. }
  11.  
  12. if (
  13. Array.isArray(value)
  14. || (typeof value === 'object' && value !== null)
  15. ) {
  16. const returnedPath = search(value, searchedValue, `${path}/${key}`);
  17. if (returnedPath !== false) {
  18. path = returnedPath;
  19. return true;
  20. }
  21. }
  22.  
  23. return false;
  24. });
  25.  
  26. return found === undefined ? false : path;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement