Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function locate(target, value, path = null){
- if(target instanceof RegExp){
- if(target.test(value)) return path
- }
- else{
- if(value === target) return path
- }
- if(Array.isArray(value)){
- return value.reduce((memo, v, i) =>
- memo || locate(target, v, path ? `${path}[${i}]` : `[${i}]`), null)
- }
- if(value === Object(value)){
- return Object.keys(value).reduce((memo, k) =>
- memo || locate(target, value[k], path ? `${/]$/.test(path) ? path : path + '.'}${k}` : k), null)
- }
- return null
- }
Add Comment
Please, Sign In to add comment