treemodel

TreeModel.parse pass array?


I'm using the TreeModel js library. It looks like the library supports passing a object that has a structure like...

{
 id: 1,
 children: [{
   id: 2,
   childre: []
 }]
}

However, what if I have a tree structure that is an array like...

[
  {
    id: 1,
    children: [],
  },
  {
    id: 2,
    children: [
      id: 5,
      children []
    ]
  }
]

Does the library not support passing an array? Thoughts on how best to deal with this?


Solution

  • You can create a fake root (id=0) whose children are your array:

    {
      id: 0,
      children: [
        {
          id: 1,
          children: [],
        },
        {
          id: 2,
          children: [
            id: 5,
            children []
          ]
        }
      ]
    }