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

High-Performance Compilers for Parallel Computing

High-Performance Compilers for Parallel Computing

Michael Wolfe / Addison-Wesley / 1995-6-16 / USD 117.40

By the author of the classic 1989 monograph, Optimizing Supercompilers for Supercomputers, this book covers the knowledge and skills necessary to build a competitive, advanced compiler for parallel or......一起来看看 《High-Performance Compilers for Parallel Computing》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具