Last active
August 6, 2016 12:54
-
-
Save mpj/f07169e8cf0fb44110b995c78f671059 to your computer and use it in GitHub Desktop.
mapTree
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
function mapTree(mapper, node) { | |
return { | |
value: mapper(node.value), | |
nodes: node.nodes | |
? node.nodes.map( x => mapTree(mapper, x)) | |
: null | |
} | |
} |
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
let familyTree = { | |
value: 'mattias', | |
nodes: [ | |
{ | |
value: 'eva', | |
nodes: [ | |
{ value: 'ove' }, | |
{ value: 'sonja' } | |
] | |
}, | |
{ | |
value: 'maths', | |
nodes: [ | |
{ value: 'anna' }, | |
{ value: 'gustav' } | |
] | |
} | |
] | |
} | |
mapTree(value => value.toUpperCase(), familyTree) | |
/* | |
OUTPUT: | |
{ | |
value: 'MATTIAS', | |
nodes: [ | |
{ | |
value: 'EVA', | |
nodes: [ | |
{ value: 'OVE' }, | |
{ value: 'SONJA' } | |
] | |
}, | |
{ | |
value: 'MATHS', | |
nodes: [ | |
{ value: 'ANNA' }, | |
{ value: 'GUSTAV' } | |
] | |
} | |
] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment