score:1

Accepted answer

i'm guessing the problem is simply that you don't declare branches inside your function, making it a global variable. so the next time the function is run, you're appending to it. adding a const declaration should probably do.

that said, i think you can simplify getbranches in a clean manner:

const getbranches = (o) => object .entries (o) .flatmap (
  ([key, {label, type, nodes}]) => [
    ... (type == types [0] ? [] : [{key, label}]), 
    ... (nodes ? getbranches (nodes) : [])
  ]
)

const types = [0, 1]
const treedata = {"first-level-node-1": {label: "regija", type: types[1], index: 0, url: "http: //opa.com", nodes: {"second-level-node-1": {label: "bolnica bn", index: 0, type: 1, nodes: {"third-level-node-1": {label: "konektor sysmex", index: 0, nodes: {}, type: types[0]}}}}}, "first-level-node-2": {label: "regija 2", index: 1, type: types[1], nodes: {"2-1": {label: "dz trebinje", index: 0, type: 1, nodes: {"3-1": {label: "konektor biomerux", index: 0, hasnodes: false, type: types[0]}}}}}}

console .log (getbranches (treedata))
.as-console-wrapper {max-height: 100% !important; top: 0}


Related Query

More Query from same tag