Guest User

Untitled

a guest
Jan 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. function locate(target, value, path = null){
  2. if(target instanceof RegExp){
  3. if(target.test(value)) return path
  4. }
  5. else{
  6. if(value === target) return path
  7. }
  8.  
  9. if(Array.isArray(value)){
  10. return value.reduce((memo, v, i) =>
  11. memo || locate(target, v, path ? `${path}[${i}]` : `[${i}]`), null)
  12. }
  13.  
  14. if(value === Object(value)){
  15. return Object.keys(value).reduce((memo, k) =>
  16. memo || locate(target, value[k], path ? `${/]$/.test(path) ? path : path + '.'}${k}` : k), null)
  17. }
  18.  
  19. return null
  20. }
Add Comment
Please, Sign In to add comment