Skip to content

Instantly share code, notes, and snippets.

@deepaktatineni
Last active September 23, 2020 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepaktatineni/34439f3ce2a63dafbca4e2dcfaa719f5 to your computer and use it in GitHub Desktop.
Save deepaktatineni/34439f3ce2a63dafbca4e2dcfaa719f5 to your computer and use it in GitHub Desktop.
Flatten children recursively
const data = require("./data.json");
const clone = ({ children, ...obj }) => obj;
const flattenJson = obj =>
obj
.flatMap(el =>
el.children ? [clone(el), ...flattenJson(el.children)] : [el]
)
// interface ParsedData {
// someFields: any
// children: ParsedData[]
// }
const parsedData = JSON.parse(JSON.stringify(data));
//finalData: Array<Omit<ParsedData, 'children'>>
const finalDat = flattenJson(parsedData);
console.log(finalDat);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment