简体   繁体   English

如何编辑树结构?

[英]How to edit tree structure?

The tree structure looks like this -树结构如下所示 -

const init = [
    {
        name: 'A',
        children: [
            {
                name: 'A1',
                children: []
            },
            {
                name: 'A2',
                children: [
                    {
                        name: 'A21',
                        children: []
                    }
                ]
            }
        ]
    },
    {
        name: 'B',
        children: [
            {
                name: 'B1',
                children: []
            },
            {
                name: 'B2',
                children: []
            }
        ]
    }
]

And I have variables我有变量

  1. currentPath = ['A', 'A2', 'A21']
  2. node = { name: 'A211', children: [] }

I want to transform init to我想将 init 转换为

const init = [
    {
        name: 'A',
        children: [
            {
                name: 'A1',
                children: []
            },
            {
                name: 'A2',
                children: [
                    {
                        name: 'A21',
                        children: [
                            {
                                name: 'A211',
                                children: []
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        name: 'B',
        children: [
            {
                name: 'B1',
                children: []
            },
            {
                name: 'B2',
                children: []
            }
        ]
    }
]

Please tell me what is the function that I need to use funcAppendNode(init, currentPath, node) that takes init, the currentPath and the new node and returns the new init.请告诉我什么是 function,我需要使用funcAppendNode(init, currentPath, node)接受 init、currentPath 和新节点并返回新的 init。 I assume it has something to do with recursion but I am unable to succeed.我认为它与递归有关,但我无法成功。

Here is what I've tried so far.这是我到目前为止所尝试的。

const funcAppend = (init, currentPath, node) => {
    let newState = [...init]
    for (let i = 1; i < currentPath.length; i++) {
        newState = newState.find(o => o.name === currentPath[i]).children
    }
    newState.push(node)
    return newState
}

The above function is returning [ { name: 'A211', children: [] } ]上面的 function 正在返回[ { name: 'A211', children: [] } ]

Please help.请帮忙。

 const init = [{ name: 'A', children: [{ name: 'A1', children: [] }, { name: 'A2', children: [{ name: 'A21', children: [] }] } ] }, { name: 'B', children: [{ name: 'B1', children: [] }, { name: 'B2', children: [] } ] } ]; const currentPath = ['A', 'A2', 'A21']; const node = { name: 'A211', children: [] }; let target = { children: init }; currentPath.forEach(path => { target = target.children.find(child => child.name === path); }); target.children.push(node); console.log(init);
 .as-console-wrapper { top: 0; max-height: 100%;important; }

I agree with recursion... so first of all, you look for the exit condition... which is path.length = 1 .我同意递归......所以首先,你寻找退出条件......这是path.length = 1 You know exactly what to do in this case.您确切地知道在这种情况下该怎么做。

If the exit condition is not satisfied you should call the function passing a new set of arguments, "reduced".如果退出条件不满足,您应该调用 function 传递一组新的 arguments,“减少”。

As similar example to your, I decided to add node to all the paths in items (not only the first one).作为与您类似的示例,我决定将节点添加到项目中的所有路径(不仅仅是第一个)。

appendNode(items, path, node) {
  if (path.length === 1) {
    for (item in items.filter(item => item.name = path[0])) {
      item.children.push(node)
    }   
  } else {
    const firstElement = path.shift(); //removed the first element from path
    for (item in items.filter(item => item.name = firstElement)) {
      addNode(item.children, path, node)
    }
  }
}

I would approach the problem this way:我会这样解决问题:

  1. from your array of names , build your tree path从您的names数组中,构建您的树路径
  2. over the tree path, just append the new node在树路径上,只是 append 新节点

 const toTreePath = ([head, ...tail], data) => { const index = data.findIndex((node) => node.name === head); const next = data[index]?.children; return [index, 'children'].concat( next?.length? toTreePath(tail, next): [] ); }; const append = (node, path, data) => { const $path = toTreePath(path, data); return R.over( R.lensPath($path), R.append(node), data, ); } // ====== const newNode = 'HELLO WORLD'; const path = ['A', 'A2', 'A21']; const data = [ { name: 'A', children: [ { name: 'A1', children: [] }, { name: 'A2', children: [ { name: 'A21', children: [] } ] } ] }, { name: 'B', children: [ { name: 'B1', children: [] }, { name: 'B2', children: [] } ] } ]; console.log( append(newNode, path, data), );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM