JSFiddle - React, Tailwind, and code Playground
by krustnic
JavaScript
const obj = {
value: 4,
next: [
{
value: 3,
next: [{
value: 7,
next: []
}]
},
{
value: 3,
next: [{
value: 7,
next: []
}, {
value: 3,
next: []
}]
}
]
}
function f(o) {
let s = o.value
if (o.next.length === 0) {
return s
}
s = o.next.reduce((acc, v) => {
acc += f(v)
return acc
}, s)
return s
}
const r = f(obj)
console.log(r)