arrToTree 转换工具 treeify

码农软件 · 软件分类 · 常用工具包 · 2019-08-12 22:42:00

软件介绍

treeify.js - v0.0.3(MIT)

treeify 是将一个数组(每个元素包含相同的特定的规则)转换为一个树对象的工具。

Constructor Parameters

treeify(data, idKey = 'id', parentIdKey = 'parentId', childrenName = 'children')
  • data {Array} 一个一维数组,该数组中每个元素均必须是一个对象,对象至少包含两个属性:

    • 唯一id的属性,如名称为 "id" 的属性;

    • 指向父级id的属性,如名称为 "parentId" 的属性。

  • idKey {string} 标识元素中哪个属性是代表唯一id的名称,默认为 "id"

  • parentIdKey {string} 标识元素中哪个属性是代表指向父元素的id,默认为 "parentId"

  • childrenName {string} 默认情况下将向元素添加一个名为 'children' 的属性,代表当前元素的子级。可通过此参数修改这个名称。

Installation

$ npm install --save-dev treeify-js

Build

$ npm run build

Usage

你有一个 arr:

var arr = [
    {
        id: 'a1',
        parentId: 'a'
    },
    {
        id: 'aq',
        parentId: 'a2'
    },
    {
        id: 'a2',
        parentId: 'a'
    },
    {
        id: 'a1-1',
        parentId: 'a1'
    },
    {
        id: 'a',
        parentId: ''
    }
];

想要转换成下边这样的 tree:

{
        id: 'a',
        parentId: '',
        children: [
            {
                id: 'a1',
                parentId: 'a',
                children: [
                    {
                        id: 'a1-1',
                        parentId: 'a1',
                        children: []
                    }
                ]
            },
            {
                id: 'a2',
                parentId: 'a',
                children: [
                    {
                        id: 'aq',
                        parentId: 'a2',
                        children: []
                    }
                ]
            }
        ]
    }

只需要这样写:

treeify(arr);

本文地址:https://codercto.com/soft/d/12239.html

Data Structures and Algorithms in Java

Data Structures and Algorithms in Java

Robert Lafore / Sams / 2002-11-06 / USD 64.99

Data Structures and Algorithms in Java, Second Edition is designed to be easy to read and understand although the topic itself is complicated. Algorithms are the procedures that software programs use......一起来看看 《Data Structures and Algorithms in Java》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换