Сообщение от Epitough
|
с помощью map(), reduce().
|
<script>
const graph = {
value: 65, children: [{
value: 15,
children: [{
value: 55,
children: [{
value: 85,
children: [{
value: 66,
children: [{ value: 29 }, { value: 21 }]
}]
}]
}]
}]
};
let example = foo(graph);
function foo(elem) {
let obj = {
max: elem.value,
min: elem.value
};
if (elem.children) {
elem.children.map(foo).reduce(
function(obj,o) {
obj.max = Math.max(obj.max, o.max);
obj.min = Math.min(obj.min, o.min);
return obj
}, obj
);
}
return obj;
}
document.write(JSON.stringify(example))
</script>