function toText(arr) {
const recur = ({Item, Children}) => Item?.Name +
(Children?.length ? "\n" + Children.map(recur).map((text, i, {length}) =>
i < length-1 ? "├──" + text.replace(/\n/g, "\n│ ")
: "└──" + text.replace(/\n/g, "\n ")
).join("\n") : "")
return arr.map(recur).join("\n");
}
let arr = [{
"Item": {
"Name": "A"
},
"Children": [
]
}, {
"Item": {
"Name": "B"
},
"Children": [{
"Item": {
"Name": "BA"
},
"Children": [{
"Item": {
"Name": "BAA"
},
"Children": [{
"Item": {
"Name": "BAAA"
}
}, {
"Item": {
"Name": "BAAB"
}
}, {
"Item": {
"Name": "BAAC"
}
}]
}, {
"Item": {
"Name": "BAB"
},
"Children": [{
"Item": {
"Name": "BABA"
}
}]
}]
}, {
"Item":{
"Name": "BB"
}
}]
}];
console.log(toText(arr));
Run code snippet