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

list-and-tree-utils

v1.0.2

Published

具有父子关系的列表数据和树形数据的处理工具

Readme

list-and-tree-utils

具有父子关系的列表数据和树形数据的处理工具。

本工具库没有进行完整细致的测试,请谨慎使用!!!

安装

npm i list-and-tree-utils

引入

import listAndTreeUtils from "list-and-tree-utils"

开始使用

测试数据:
const list = [
    {
        id: 1,
        parentId: 0,
        name: "一级 1",
    },
    {
        id: 2,
        parentId: 1,
        name: "二级 1-1",
    },
    {
        id: 3,
        parentId: 2,
        name: "三级 1-1-1",
    },
    {
        id: 4,
        parentId: 0,
        name: "一级 2",
    },
    {
        id: 5,
        parentId: 4,
        name: "二级 2-1",
    },
    {
        id: 6,
        parentId: 5,
        name: "三级 2-1-1",
    },
    {
        id: 7,
        parentId: 4,
        name: "二级 2-2",
    },
    {
        id: 8,
        parentId: 7,
        name: "三级 2-2-1",
    },
    {
        id: 9,
        parentId: 0,
        name: "一级 3",
    },
    {
        id: 10,
        parentId: 9,
        name: "二级 3-1",
    },
    {
        id: 11,
        parentId: 10,
        name: "三级 3-1-1",
    },
    {
        id: 12,
        parentId: 9,
        name: "二级 3-2",
    },
    {
        id: 13,
        parentId: 12,
        name: "三级 3-2-1",
    },
    {
        id: 14,
        parentId: 12,
        name: "三级 3-2-2",
    },
]
测试数据:
const tree = [
    {
        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"
                    },
                ],
            },
        ],
    },
]
/**
 * @description 组装树结构
 *
 * @param {Array} list 具有父子关系的列表数据
 * @param {Object} options 配置项
 *
 * @param {String} options.idField id字段名,默认为{id}
 * @param {String} options.parentIdField 父id字段名,默认为{parentId}
 * @param {String} options.topNodeParentId 顶级节点的父id,默认为{0}
 * @param {String} options.childrenField 子节点字段名,默认为{children}
 * @param {Object} options.emptyChildrenValue 空子节点的值,默认为{undefined}
 * @param {Function} options.eachNode 遍历每个节点的回调函数,参数为(node, children)
 * @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
 */
listAndTreeUtils.list.toTree(list, options);


使用示例:
const eachNode = (node, children) => {
    node['label'] = node.name;
    node['isLeaf'] = children.length === 0;
}
const result = listAndTreeUtils.list.toTree(list, {
    emptyChildrenValue: [],
    eachNode,
})
console.log(result)
/**
 * @description 拆解树结构
 *
 * @param {Array} tree 树结构数据
 * @param {Object} options 配置项
 *
 * @param {String} options.idField id字段名,默认为{id}
 * @param {String} options.parentIdField 父id字段名,默认为{parentId}
 * @param {String} options.topNodeParentId 顶级节点的父id,默认为{0}
 * @param {String} options.childrenField 子节点字段名,默认为{children}
 * @param {Boolean} options.remainChildren 是否保留子节点,默认为{false}
 * @param {Function} options.eachNode 遍历每个节点的回调函数,参数为(node, children)
 * @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
 */
listAndTreeUtils.tree.toList(tree, options);
    

使用示例:
const eachNode = (node, children) => {
    node['label'] = node.name;
    node['isLeaf'] = children.length === 0;
}
const result = listAndTreeUtils.tree.toList(tree, {
    emptyChildrenValue: [],
    eachNode,
})
console.log(result)
/**
 * @description 在列表中过滤目标节点,返回所有目标节点以及该节点的所有父节点
 *
 * @param {Function} filter 过滤函数
 * @param {Array} list 具有父子关系的列表数据
 * @param {Object} options 配置项
 *
 * @param {String} options.idField id字段名,默认为{id}
 * @param {String} options.parentIdField 父id字段名,默认为{parentId}
 * @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
 */
listAndTreeUtils.list.filter(filter, list, options);


使用示例:
const filter = item => item.name.indexOf("1-") > -1
const result = listAndTreeUtils.list.filter(filter, list)
console.log(result)
/**
 * @description 获取目标节点的所有父节点
 *
 * @param {Number} targetNodeId 目标节点id
 * @param {Array} list 具有父子关系的列表数据
 * @param {Object} options 配置项
 *
 * @param {String} options.idField id字段名,默认为{id}
 * @param {String} options.parentIdField 父id字段名,默认为{parentId}
 * @param {Boolean} options.returnTargetNode 目标节点是否一起返回,默认为{true}
 * @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
 */
listAndTreeUtils.list.getParents(targetNodeId, list, options)


使用示例:
const result = listAndTreeUtils.list.getParents(3, list)
console.log(result)
/**
 * @description 获取目标节点的所有子节点
 *
 * @param {Number} targetNodeId 目标节点id
 * @param {Array} list 具有父子关系的列表数据
 * @param {Object} options 配置项
 *
 * @param {String} options.idField id字段名,默认为{id}
 * @param {String} options.parentIdField 父id字段名,默认为{parentId}
 * @param {Boolean} options.returnTargetNode 目标节点是否一起返回,默认为{true}
 * @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
 */
listAndTreeUtils.list.getChildren(targetNodeId, list, options)


使用示例:
const result = listAndTreeUtils.list.getChildren(1, list)
console.log(result)