Last active
April 6, 2021 12:15
-
-
Save seoh/1264abf2bd3b6371d5ea8816cceaea87 to your computer and use it in GitHub Desktop.
find key pathes contains keyword in value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inspect(obj, keyword, curr) { | |
if(!obj) return [] | |
const keys = Object.keys(obj) | |
let ret = [] | |
for(let i=0; i<keys.length; i++) { | |
const value = obj[keys[i]] | |
if(typeof value == 'object') { | |
ret = [].concat.apply(ret, | |
inspect(value, keyword, Array.isArray(obj) | |
? curr + '[' + keys[i] + ']' | |
: curr + '.' + keys[i])) | |
} else { | |
if(typeof value == 'string' && value.indexOf(keyword) > -1) | |
ret.push(curr + '.' + keys[i]) | |
} | |
} | |
return ret | |
} | |
inspect(require('./response.json'), 'Error', '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment