SHARE
TWEET

Untitled

a guest Aug 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top