Skip to content

Instantly share code, notes, and snippets.

@rmuratov
Last active February 10, 2017 13:57
Show Gist options
  • Save rmuratov/e65ec03cbc20cd3f6ba72d0e609e21e4 to your computer and use it in GitHub Desktop.
Save rmuratov/e65ec03cbc20cd3f6ba72d0e609e21e4 to your computer and use it in GitHub Desktop.
input =
a:
b: 'one'
d: 'two'
f: g: 'three'
bb: 'four'
dd: 'five'
gg:
jj: 'six'
as: 'seven'
dfh: wsg: seg: wet:
ger: 'eight'
hhh: 'nine'
do goDeeper = (obj = input, prevKey = '') ->
for key, val of obj
currKey = "#{prevKey}#{key}"
switch typeof val
when 'string'
console.log currKey
when 'object'
goDeeper val, "#{currKey}."
return
###
'a.b',
'a.d',
'a.f.g',
'bb',
'dd',
'gg.jj',
'gg.as',
'gg.dfh.wsg.seg.wet.ger',
'gg.dfh.wsg.seg.wet.hhh'
###
@rmuratov
Copy link
Author

The same in python3

def get_keys(obj):
    result = []

    def go_deeper(obj, prev_key=''):
        for key, val in obj.items():
            curr_key = prev_key + key
            if isinstance(val, str):
                result.append(curr_key)
            elif isinstance(val, dict):
                go_deeper(val, curr_key + '.')
    go_deeper(obj)
    return result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment