Skip to content

Instantly share code, notes, and snippets.

@dobernike
Last active October 30, 2021 20:55
Show Gist options
  • Select an option

  • Save dobernike/8cea32e0df24c74b6cdd213c34ddc89e to your computer and use it in GitHub Desktop.

Select an option

Save dobernike/8cea32e0df24c74b6cdd213c34ddc89e to your computer and use it in GitHub Desktop.
flatten object typescript and ramda
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