I've been using treeify recently to display a nested JSON object as a tree, like the output of Linux CLI tool tree
.
Is the same result doable with only JSON.stringify
? I know JSON.stringify accepts 3 arguments: value, replacer and space (MDN JSON.strinfify documentation).
JSON.strinfify(value, replacer, space)
I ask the question as I discovered the space could be a String, and not only a Number.
The solution should be as generic as possible, but if any example is needed, refer to the one displaid on treeify page:
const x = {
oranges: { mandarin: { clementine: null, tangerine: 'so cheap and juicy!' } },
apples: { gala: null, 'pink lady': null },
others: [ 'bananas', 'ananas', 'pear' ]
}
->
> console.log( treeify.asTree(x, true, true) )
├─ oranges
│ └─ mandarin
│ ├─ clementine
│ └─ tangerine: so cheap and juicy!
├─ apples
│ ├─ gala
│ └─ pink lady
└─ others
├─ 0: bananas
├─ 1: ananas
└─ 2: pear
Is it possible to access the space
argument from the replacer
function as a starting point?