Skip to content

Instantly share code, notes, and snippets.

@otonielguajardo
Created October 13, 2021 00:13
Show Gist options
  • Save otonielguajardo/c0aa75b2e12e42137c96fc1fa1c93a88 to your computer and use it in GitHub Desktop.
Save otonielguajardo/c0aa75b2e12e42137c96fc1fa1c93a88 to your computer and use it in GitHub Desktop.
Iterate and map each object in a nested array
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