Last active
September 23, 2020 11:35
-
-
Save deepaktatineni/34439f3ce2a63dafbca4e2dcfaa719f5 to your computer and use it in GitHub Desktop.
Flatten children recursively
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
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