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

json-tree-utils

v1.1.9

Published

json数据与树形数据的转换等处理工具

Readme

json-tree-utils

此工具库不再更新!!!请使用最新的工具库:

https://www.npmjs.com/package/list-and-tree-utils

json数组、树形数据的处理工具

安装


npm i json-tree-utils

引入


import jtUtils from "json-tree-utils"

准备点测试数据

 const list = [
     {id: 1, name: "一级 1", parentId: 0},
     {id: 2, name: "二级 1-1", parentId: 1},
     {id: 3, name: "三级 1-1-1", parentId: 2},
     {id: 4, name: "一级 2", parentId: 0},
     {id: 5, name: "二级 2-1", parentId: 4},
     {id: 6, name: "三级 2-1-1", parentId: 5},
     {id: 7, name: "二级 2-2", parentId: 4},
     {id: 8, name: "三级 2-2-1", parentId: 7},
     {id: 9, name: "一级 3", parentId: 0},
     {id: 10, name: "二级 3-1", parentId: 9},
     {id: 11, name: "三级 3-1-1", parentId: 10},
     {id: 12, name: "二级 3-2", parentId: 9},
     {id: 13, name: "三级 3-2-1", parentId: 12},
     {id: 14, name: "三级 3-2-2", parentId: 12},
 ]

 const treeData = [{
     id:1,
     name: '一级 1',
     children: [{
         id:2,
         name: '二级 1-1',
         children: [{
             id:3,
             name: '三级 1-1-1'
         }]
     }]
 }, {
     id:4,
     name: '一级 2',
     children: [{
         id:5,
         name: '二级 2-1',
         children: [{
             id:6,
             name: '三级 2-1-1'
         }]
     }, {
         id:7,
         name: '二级 2-2',
         children: [{
             id:8,
             name: '三级 2-2-1'
         }]
     }]
 }, {
     id:9,
     name: '一级 3',
     children: [{
         id:10,
         name: '二级 3-1',
         children: [{
             id:11,
             name: '三级 3-1-1'
         }]
     }, {
         id:12,
         name: '二级 3-2',
         children: [{
             id:13,
             name: '三级 3-2-1'
         },{
             id:14,
             name: '三级 3-2-2'
         }]
     }]
 }];  

组装树结构(json数组转成树形数据)


 jtUtils.json.parse(data, options);

 data: json数组
 options: 可选配置项

 默认 options = {        
    idField: 'id', // 指定项。节点唯一标识的字段,整棵树中是唯一的
    parentIdField: 'parentId', // 指定项。父节点存放的字段
    topNodeParentId: 0, // 指定项。顶级节点的父id的值
    childrenField: 'children', // 设置项。子节点存放的字段
    handleNode: (node, children)=>{} // 设置项。每个节点的回调处理
 }


 使用示例:
 
 const handleNode = (node, children) => {
     node['label'] = node.name;
     node['isLeaf'] = children.length === 0;
 }

 const data = jtUtils.json.parse(list, {
     handleNode
 })

 console.log(JSON.stringify(data))

拆解树结构(树形数据转成json数组)

 
 jtUtils.tree.jsonify(data, options);
 
 data: 树形数据
 options: 可选配置项
 
 默认 options = {        
     idField: 'id', // 设置项。节点唯一标识的字段,整棵树中是唯一的
     parentIdField: 'parentId', // 设置项。父节点存放的字段
     topNodeParentId: 0, // 设置项。顶级节点的父id的值
     childrenField: 'children', // 指定项。子节点存放的字段
     remainChildren: false, // 设置项。是否保留每个节点的子节点
     handleNode: null // 设置项。每个节点的回调处理
 }
 
 
 使用示例:
 
 const handleNode = (node, children) => {
     node['label'] = node.name;
     node['isLeaf'] = children.length === 0;
 }
 
 const data = jtUtils.tree.jsonify(treeData, {
     handleNode
 })
 
 console.log(JSON.stringify(data))
 

过滤树节点(返回所有目标节点以及该节点的所有父节点)

 
 jtUtils.json.filter(data, handleFilter,options)
 
 data: json数组
 handleFilter: node的过滤处理,return的节点为目标节点
 options: 可选配置项
 
 默认 options = {        
     idField: 'id', // 指定项。节点唯一标识的字段,整棵树中是唯一的
     parentIdField: 'parentId', // 指定项。父节点存放的字段
 }
 
 
 使用示例:
 
 const handleFilter = (node) => {
     if (node.name.indexOf('2-2') !== -1) {
         node['isTargetNode'] = true;
         return node
     }
 }

 const data = jtUtils.json.filter(list, handleFilter)

 console.log(JSON.stringify(data))
 

查找目标节点的父节点ID(返回某目标节点的所有父节点id的数组)

 
 jtUtils.json.findParentIds(id, data, options)
 
 id: 目标节点的id值
 data: json数组
 options: 可选配置项
 
 默认 options = {        
     idField: 'id', // 指定项。节点唯一标识的字段,整棵树中是唯一的
     parentIdField: 'parentId', // 指定项。父节点存放的字段
     remainNode: false, //设置项。是否将目标节点ID一起返回
 }
 
 
 使用示例:
 
 const data = jtUtils.json.findParentIds(13, list, {remainNode: true})
 
 console.log(JSON.stringify(data))
 

查找目标节点的父节点数据(返回某目标节点的所有父节点数据的数组)

 
 jtUtils.json.findParents(id, data, options)
 
 id: 目标节点的id值
 data: json数组
 options: 可选配置项
 
 默认 options = {        
     idField: 'id', // 指定项。节点唯一标识的字段,整棵树中是唯一的
     parentIdField: 'parentId', // 指定项。父节点存放的字段
     remainNode: false, //设置项。是否将目标节点ID一起返回
 }
 
 
 使用示例:
 
 const data = jtUtils.json.findParents(13, list, {remainNode: true})
 
 console.log(JSON.stringify(data))
 

查找目标节点的子节点ID(返回某目标节点的所有子节点id的数组)

 
 jtUtils.json.findChildrenIds(id, data, options)
 
 id: 目标节点的id值
 data: json数组
 options: 可选配置项
 
 默认 options = {        
     idField: 'id', // 指定项。节点唯一标识的字段,整棵树中是唯一的
     parentIdField: 'parentId', // 指定项。父节点存放的字段
     remainNode: false, //设置项。是否将目标节点ID一起返回
 }
 
 
 使用示例:
 
 const data = jtUtils.json.findChildrenIds(12, list, {remainNode: true})
 
 console.log(JSON.stringify(data))
 

查找目标节点的子节点数据(返回某目标节点的所有子节点数据的数组)

 
 jtUtils.json.findChildrens(id, data, options)
 
 id: 目标节点的id值
 data: json数组
 options: 可选配置项
 
 默认 options = {        
     idField: 'id', // 指定项。节点唯一标识的字段,整棵树中是唯一的
     parentIdField: 'parentId', // 指定项。父节点存放的字段
     remainNode: false, //设置项。是否将目标节点ID一起返回
 }
 
 
 使用示例:
 
 const data = jtUtils.json.findChildrens(12, list, {remainNode: true})
 
 console.log(JSON.stringify(data))