Home  >  Q&A  >  body text

javascript - How to query or delete a node in a tree structure by id in js

Ant design requires this kind of data structure to form various tree controls. This is really not as good as ztree and the like. People only need one array. After specifying the id and parentId, the tree will automatically appear. antd also I need to get children, I don’t think it’s any good

For example, the tree structure is like this

[
    {
        id: '1',
        name: 'a',
        pid: '0',
        children: [
            {
                id: '11',
                name: 'a-1',
                pid: '1',
                children: [
                    id: '111',
                    name: 'a-1-1',
                    pid: '11',
                    children: null
                ]
            }
        ]
    },
    {
        id: '2',
        name: 'b',
        pid: '0',
        children: null,
    }
]

So if we can quickly find the node with id: '111', can we only use recursive query? Is there a better way?
Perform the following operations on it:
Add child nodes: that is, in his children Add
to delete the node: that is, delete the node with id: '111'
Update node

欧阳克欧阳克2663 days ago1218

reply all(1)I'll reply

  • 三叔

    三叔2017-07-05 11:07:50

    Add a conversion layer and use id as the index for the reconstructed data

    reply
    0
  • Cancelreply