Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global.find = re => json => [...find(json, re)]
- function* find(v, regex, path = '') {
- if (regex.test(path)) {
- yield path
- return
- }
- if (typeof v === 'undefined' || v === null) {
- return
- }
- if (Array.isArray(v)) {
- let i = 0
- for (let value of v) {
- const prefix = (path === '' ? 'this' : path)
- yield* find(value, regex, prefix + '[' + i++ + ']')
- }
- return
- }
- if (typeof v === 'object' && v.constructor === Object) {
- const entries = Object.entries(v)
- for (let [key, value] of entries) {
- yield* find(value, regex, path + '.' + key)
- }
- return
- }
- if (regex.test(v)) {
- yield path
- }
- }
Add Comment
Please, Sign In to add comment