js Array to Tree

Posted jkhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js Array to Tree相关的知识,希望对你有一定的参考价值。

source = [{
                id: 1,
                pid: 0,
                name: ‘body‘
            }, {
                id: 2,
                pid: 1,
                name: ‘title‘
            }, {
                id: 3,
                pid: 2,
                name: ‘div‘
            }]
            转换为: [{
                        id: 1,
                        pid: 0,
                        name: ‘body‘,
                        children: [{
                                id: 2,
                                pid: 1,
                                name: ‘title‘,
                                children: [{
                                    id: 3,
                                    pid: 1,
                                    name: ‘div‘
                                }]
                            }
                        }]

 

技术图片
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=`, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>toTree</title>
    </head>

    <body>
        <script>
            var source = [{
                id: 1,
                pid: 0,
                name: ‘body‘
            }, {
                id: 2,
                pid: 1,
                name: ‘title‘
            }, {
                id: 3,
                pid: 1,
                name: ‘div‘
            }, {
                id: 4,
                pid: 3,
                name: ‘span‘
            }, {
                id: 5,
                pid: 3,
                name: ‘icon‘
            }, {
                id: 6,
                pid: 4,
                name: ‘subspan‘
            }]

            function toTree(data) {
                let result = []
                if(!Array.isArray(data)) {
                    return result
                }
                data.forEach(item => {
                    delete item.children;
                });
                let map = {};
                data.forEach(item => {
                    map[item.id] = item;
                });
                data.forEach(item => {
                    let parent = map[item.pid];
                    if(parent) {
                        (parent.children || (parent.children = [])).push(item);
                    } else {
                        result.push(item);
                    }
                });
                return result;
            }
            console.log(toTree(source))
        </script>
    </body>

</html>
解决办法

以上是关于js Array to Tree的主要内容,如果未能解决你的问题,请参考以下文章

108. Convert Sorted Array to Binary Search Tree

js代码片段: utils/lcoalStorage/cookie

[LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree

[Leetcode] convert sorted array to binary search tree

108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree