@abala/arr2tree

array to tree ## Installation ```bash npm i @abala/arr2tree ```

Usage no npm install needed!

<script type="module">
  import abalaArr2tree from 'https://cdn.skypack.dev/@abala/arr2tree';
</script>

README

arr2tree

array to tree

Installation

npm i @abala/arr2tree

Usage

const array = [
    {
        id: 'parent-1',
        value: '水果',
    },
    {
        id: 'children-1',
        value: '橘子',
        parentId: 'parent-1'
    },
    {
        id: 'children-2',
        value: '苹果',
        parentId: 'parent-1'
    },
    {
        id: 'children-3',
        value: '香蕉',
        parentId: 'parent-1'
    },
]

arr2tree(array)

// 输出
// [
//     {
//         id: 'parent-1',
//         value: '水果',
//         children: [
//             {
//                 id: 'children-1',
//                 value: '橘子',
//                 parentId: 'parent-1'
//             },
//             {
//                 id: 'children-2',
//                 value: '苹果',
//                 parentId: 'parent-1'
//             },
//             {
//                 id: 'children-3',
//                 value: '香蕉',
//                 parentId: 'parent-1'
//             },
//         ]
//     }
//]

API

| 属性 | 说明 | 类型 | 默认值 | --- | --- | --- | -- | | parentId | 父元素id | string| 'parentId' | id | 元素id | string | 'id' | array | 数据 | array | []