Skip to content

Instantly share code, notes, and snippets.

@javascripto
Created July 31, 2021 09:23
Show Gist options
  • Select an option

  • Save javascripto/a2d1d21fe49f0675d5ce7fb1e2cbf363 to your computer and use it in GitHub Desktop.

Select an option

Save javascripto/a2d1d21fe49f0675d5ce7fb1e2cbf363 to your computer and use it in GitHub Desktop.
Deep search for term in object keys and values. It logs the path to access the item
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n)
}
function searchTermInObjectAndPrintPath(object, text, path = '') {
text = text.toLowerCase()
for (let [key, value] of Object.entries(object)) {
if (key.toLowerCase().includes(text)) {
// encontrou na key
console.log(path)
}
if (typeof value !== 'object') {
value = String(value)
if (value.toLowerCase().includes(text)) {
// encontrou no value
console.log(path)
}
} else {
if (isNumber(key)) {
searchTermInObjectAndPrintPath(value, text, `${path}[${key}]`)
} else {
searchTermInObjectAndPrintPath(value, text, `${path}.${key}`)
}
}
}
}
// https://www.youtube.com/feed/trending?persist_gl=1&gl=BR
// searchTermInObjectAndPrintPath(ytInitialData, 'nome do video')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment