npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

hd-tree-data

v0.0.2

Published

树型数据转换

Downloads

5

Readme

Hd-Tree-Data

树接口数据转换

快速开始

下载


# 安装插件
npm install hd-tree-data

引入

// 引入插件
import Tree from 'hd-tree-data'
import { Tree,recursion , listToTree, treeToList, listToObj } from 'hd-tree-data'

初始化,返回一个实例

// 传入配置参数,返回一个实例
const tree = new Tree({
  idName: 'id',
  childrenName: 'children',
  parentIdName: 'pid',
  rootId: [0],
  data: [],
  type: 'list'
})

Tree实例可选配置参数如下:

参数名 | 说明 | 值类型 | 默认值 --- | --- | --- | --- data | 原始数据tree结构或者list结构 | Array | 无 type | 'list'或 'tree' | String | 无 idName | 节点唯一标识符 | Number | id parentIdName | 父级节点唯一标识符 | Boolean | pId childrenName | 子节点属性名 | Boolean | children rootId | 根节点的值,满足其中之一为根节点 | Array | [null, undefined, 0, '0', null]

返回数据: 数据名 | 说明 | 值类型 --- | --- | --- tree | 多维数组 | [] list | 一维数组 | [] objList | 以id为key的object | {}

递归方法recursion({data,recursionCondition,level},callback)

参数名 | 说明 | 值类型 --- | --- | --- data | 需要递归的数据 | [] 或者 {} recursionCondition | 判断条件,会抛出当前项item以及父级的数据data | Function level | 首层的level 默认0 | number callback| 见callback说明 | function

参数名 | 说明 | 值类型 --- | --- | --- item | 当前节点的Item | any level | 层级 | Number parent | 父级的data | []或{} parentIdName| 父级idname parent和parentIdName 结合起来能找到自 | string

callback return false会终止该叶子的递归,return数组或者object的时候回继续递归