Skip to content

Instantly share code, notes, and snippets.

@gunzip
Created September 21, 2017 20:32
Show Gist options
  • Save gunzip/50166a485d7bc47987e187248d550073 to your computer and use it in GitHub Desktop.
Save gunzip/50166a485d7bc47987e187248d550073 to your computer and use it in GitHub Desktop.
flatten object with ramda
import { chain, fromPairs, map, toPairs } from "ramda";
export const flattenObj = (obj: {}) => {
// tslint:disable-next-line:no-any
const go = (objX: {}): any =>
chain(([k, v]: [{}, {}]) => {
if (typeof v === "object") {
return map(([kX, vX]) => [`${k}.${kX}`, vX], go(v));
} else {
return [[k, v]];
}
}, toPairs(objX));
return fromPairs(go(obj));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment