Have ideas to improve npm?Join in the discussion! »

@abala/arr2tree

1.1.1 • Public • Published

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 []

Keywords

none

Install

npm i @abala/arr2tree

DownloadsWeekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

5.56 kB

Total Files

13

Last publish

Collaborators

  • avatar