3

Is there any tree view component support display any arbitrary JavaScript object like:

{
   foo: 'bar'
   zee: [1, 2, 3]
}

Which result in the following view:

/----foo 
  |   |
  |   |-- 'bar'
  |
  |--zee
      |
      |--
        |--1
        |--2
        |--3

Note the above ascii tree is just for demonstration of the tree structure, not necessarily the final result.

0

4 Answers 4

2

const Tree=(o,m=o,v='')=>{
    for(e in o){
        if(typeof o!=='object'){console.log(v+'┗╸'+o);return}
        Array.isArray(o)?!Array.isArray(o[e])?console.log(v+(o.length===1||o[e]===o[o.length-1]?'┗╸':'┣╸')+o[e]):Tree(o[e],m,v+(o.length===1||o[e]===o[o.length-1]?' '.repeat(o[e].toString.length+1):'┃'+' '.repeat(e.length))):(console.log(v+(e===Object.keys(m)[0]?'┏╸':Object.keys(o).length===1||Object.keys(o)[Object.keys(o).length-1]===e?'┗╸':'┣╸')+e),Tree(typeof o[e]!=='object'?String(o[e]):o[e],m,v+(Object.keys(o).length===1||Object.keys(o)[Object.keys(o).length-1]===e?' '.repeat(e.length+1):'┃'+' '.repeat(e.length))))
    }
}
//Usage : Tree(YourObjHere)

//Example :
  Tree({
     foo: 'bar',
     zee: [1, 2, 3]
  }) 

//Let me know if bugs

Sign up to request clarification or add additional context in comments.

Comments

0

It's unclear as to your exact need but this might suit you well: http://www.jstree.com/documentation/json_data

1 Comment

This doesn't work for me. coz I want to create a tree to show the structure of any arbitrary object, like what you got in chrome's console when you have console.log(myObj).
0

I found a small library that will display JSON like you want. JSON is pretty much a Javascript object so it should work for you needs (worked for mine).

https://github.com/lmenezes/json-tree

Comments

0

A very good tree view component package to create a tree structure as exactly you require in the following link. So Visit https://www.npmjs.com/package/treeify

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.