Created
July 31, 2021 09:23
-
-
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
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 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