Last active
October 30, 2021 20:55
-
-
Save dobernike/8cea32e0df24c74b6cdd213c34ddc89e to your computer and use it in GitHub Desktop.
flatten object typescript and ramda
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
| import { chain, fromPairs, map, toPairs, type } from 'ramda' | |
| export const flattenObj = (obj: Record<string | number, string>) => { | |
| const go = (obj_: Record<string | number, string>): any[] => | |
| chain(([k, v]: [k: string, v: Record<string | number, string>]) => { | |
| if (type(v) === 'Object' || type(v) === 'Array') { | |
| return map(([k_, v_]: [k_: string, v_: string]) => [`${k}.${k_}`, v_], go(v)) | |
| } | |
| return [[k, v]] | |
| }, toPairs(obj_)) | |
| return fromPairs(go(obj)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment