Created
October 13, 2021 00:13
-
-
Save otonielguajardo/c0aa75b2e12e42137c96fc1fa1c93a88 to your computer and use it in GitHub Desktop.
Iterate and map each object in a nested array
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
export const mapNested = (nodes: any, mapCallback: any) => { | |
function recursive(nodes: any) { | |
return nodes.map((node: any) => { | |
return recursiveSelector(node, recursive, mapCallback) | |
}); | |
} | |
return recursive(nodes); | |
} | |
const recursiveSelector = (node: any, recursive: any, map: any) => { | |
if (node.children) node.children = recursive(node.children); | |
return map(node); | |
} | |
// mapNested(collection, (item) => { return { ...item, hello: 'world' } }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment