Skip to content

Instantly share code, notes, and snippets.

@seoh
Last active April 6, 2021 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seoh/1264abf2bd3b6371d5ea8816cceaea87 to your computer and use it in GitHub Desktop.
Save seoh/1264abf2bd3b6371d5ea8816cceaea87 to your computer and use it in GitHub Desktop.
find key pathes contains keyword in value
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