Last active
February 11, 2019 21:54
-
-
Save bugb/b17d21d704ed454709e1415e0806d262 to your computer and use it in GitHub Desktop.
Get all nested keys of an Object (Not includes values is an array)
This file contains 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
f = (x, st = []) => Object.keys(x).reduce((m, n) => { | |
s = [...st, n]; | |
if (typeof x[n] == 'object') { | |
return [...m, ...f(x[n], s)] | |
f(x[n], s); | |
} else return [...m, s.join `.`] | |
}, []) | |
user = { | |
id: 101, | |
email: 'jack@dev.com', | |
personalInfo: { | |
name: 'Jack', | |
address: { | |
line1: 'westwish st', | |
line2: 'washmasher', | |
city: 'wallas', | |
state: 'WX' | |
}, | |
age: 200 | |
} | |
} | |
f(user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With object includes array as value:
Credit: https://gist.github.com/JamieMason/4be6028de0608d5854c0
Performs a deeply recursive reduce on a JSON-encodable JavaScript Object hierarchy.
Example
The following iterator flattens a deeply nested JavaScript object hierarchy into a single object.
Input
When
data
is;Output
Our example produces the following output;