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 🙏

© 2024 – Pkg Stats / Ryan Hefner

tree-as-array

v3.0.16

Published

Use array methods to process tree-structured data

Downloads

361

Readme

介绍

用数组的方法操作树形结构数据。目前已支持mapTree、filterTree、findTree、pushTree、unshiftTree、popTree、shiftTree、someTree、everyTree、atTree、indexOfTree和特殊方法atIndexOfTree、nodeDepthMap和dedupTree。每个方法的最后一个参数可以自定义children和id的属性名。

安装教程

yarn add tree-as-array
npm i tree-as-array

使用说明

引入

import t from 'tree-as-array'

mapTree(遍历树结构数据的方法)

t.mapTree(treeData, (item) => {
    console.log(item)
})

filterTree(树结构数据的filter方法)

const values = ['node1', 'node2','node3'];
const result = t.filterTree(treeData, (item) => {
    return values.includes(item.name)
})
 
console.log(result)

findTree(树结构数据的find方法)

const result = t.findTree(treeData, (item) => {
    return item.hasOwnProperty('children')
})
 
console.log(result)

pushTree(targetParentId为目标节点的id,newNode为往该节点添加的数据)

t.pushTree(treeData, targetParentId, newNode);

console.log(treeData)

unshiftTree(targetParentId为目标节点的id,newNode为往该节点添加的数据)

t.unshiftTree(treeData, targetParentId, newNode);

console.log(treeData)

popTree(rootId为目标节点的id,此方法可删除rootId下的最后一个子节点)

t.popTree(treeData, rootId);

console.log(treeData)

shiftTree(rootId为目标节点的id,此方法可删除rootId下的第一个子节点)

t.shiftTree(treeData, rootId);

console.log(treeData)

someTree(树结构数据的some方法)

const result = t.someTree(treeData, item => item.name === 'jack')

console.log(result)

everyTree(树结构数据的every方法))

const result = t.everyTree(treeData, item => item.age >= 18)

console.log(result)

atTree(parentId为指定父节点的id,nodeIndex为子节点的索引,可传负数,和数组的at方法一样)

const result = t.atTree(treeData, parentId, nodeIndex)

console.log(result)

indexOfTree(返回一个数组,值为从根节点开始到targetId所在节点的索引,返回值可以传入atIndexOfTree的第二个参数进行取值)

const result = t.indexOfTree(treeData, targetId)

console.log(result)

atIndexOfTree(传入节点数据的下标数组,返回节点数据)

const result = t.atIndexOfTree(treeData, [0, 1, 0])

console.log(result)

nodeDepthMap(返回一个字典,键代表节点的id,值代表该节点在数据的第几层)

const result = t.nodeDepthMap(treeData)

console.log(result)

dedupTree(树结构对象数组去重方法,第一个参数为需要去重的数据,第二个参数为以哪个键去重)

const result = t.dedupTree(treeData, 'id')

console.log(result)