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 🙏

© 2026 – Pkg Stats / Ryan Hefner

to-json-tree

v1.1.1

Published

Convert JSON to JSON Tree

Readme

to-json-tree

Convert JSON to JSON tree (将具有层级关系的扁平结构JSON数据转换为JSON树)

Installing

Using npm:

$ npm install to-json-tree

Import

Using import:

import toJsonTree from 'to-json-tree'

Using require:

const toJsonTree = require('to-json-tree')

Example

/*
toJsonTree用于将具有层级关系的扁平结构数据转换成json树
1.调用方法:toJsonTree(oData,param);
2.关于oData和params;
  1)oData即带有层级关系的原数据,它是一个数组,为必传参数
  2)params为非必传参数,它的默认值为{ rootKey: 'id', rootVal: 0, selfKey: 'id', parentKey: 'parentId' }   
    其中rootKey和rootVal分别表示能确定某元素为树的根元素的key值和value值,selfKey和parentKey分别表示某元素能定   
    位自身和父元素的key值;当你的数据与与默认参数不一致时,你可以参考参考示例2进行配置
3.方法返回值中banch代表json树中往上生长的树枝
*/

// 示例1(原数据参数与默认参数一致)
var oData1 = [
  { id: 0, parentId: 0, text: '000' },
  { id: 1, parentId: 0, text: '111' },
  { id: 2, parentId: 1, text: '222' },
  { id: 3, parentId: 1, text: '333' },
  { id: 4, parentId: 2, text: '444' }
];
var res1 = toJsonTree(oData1);
console.log('res1', res1);

// 示例2(原数据参数与默认参数不一致)
var oData2 = [
  { id: 6, iType: 'root', pId: 6, text: '666' },
  { id: 1, iType: 'not_root', pId: 6, text: '111' },
  { id: 2, iType: 'not_root', pId: 1, text: '222' },
  { id: 3, iType: 'not_root', pId: 1, text: '333' },
  { id: 4, iType: 'not_root', pId: 2, text: '444' }
];
var param = { rootKey:'iType', rootVal:'root', parentKey:'pId' };//哪个不匹配就配置哪个
var res2 = toJsonTree(oData2,param);
console.log('res2', res2);