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

cng-tree-utils

v0.0.2

Published

for javascript convert array to tree and revert

Downloads

11

Readme

Tree utils for javascript.

In most projects, the use of tree structures to simplify data structures, expand or collapse menus. This library gives you tools to convert an array to a tree, and vice versa. In addition, also supports the arrangement by tree structure, calculate the values of subtracting leaves on tree branches and stumps ...

to use in javascript in browser or angluar or react or nodejs


npm install cng-tree-utils

# test for array to tree or array sort by tree
node ./tree-utils/test/test-array-2-tree.js

# test for array to order by tree
node ./tree-utils/test/test-array-2-order-by-tree.js 


# test for tree 2 array
node ./tree-utils/test/test-tree-2-array.js 

Make array to tree

const test = require("cng-tree-utils");

let arr = [
    {
        id: 1
        , name: "A"
        , total: 0
    },
    {
        id: 2
        , name: "B"
        , total: 1
    },
    {
        id: 3
        , parent: 2
        , name: "B.1"
        , total: 2
    },
    {
        id: 4
        , parent: 1
        , name: "A.1"
        , total: 3
    },
    {
        id: 5
        , parent: 2
        , name: "B.2"
        , total: 4
    },
    {
        id: 6
        , parent: 3
        , name: "B.1.1"
        , total: 5
    },
];

// print orgin array
console.log("Origin ARRAY ***>\n", JSON.stringify(arr, null, 2));

// convert array to tree
let arr2Tree = test.array2Tree(arr, "id", "parent");
console.log("array to TREE -->\n", JSON.stringify(arr2Tree, null, 2));

Sort Array by Tree structure:

const test = require("cng-tree-utils");

let arr = [
    {
        id: 1
        , name: "A"
        , total: 0
    },
    {
        id: 2
        , name: "B"
        , total: 1
    },
    {
        id: 3
        , parent: 2
        , name: "B.1"
        , total: 2
    },
    {
        id: 4
        , parent: 1
        , name: "A.1"
        , total: 3
    },
    {
        id: 5
        , parent: 2
        , name: "B.2"
        , total: 4
    },
    {
        id: 6
        , parent: 3
        , name: "B.1.1"
        , total: 5
    },
];

// print origin Array
console.log("Origin ARRAY ***>\n", JSON.stringify(arr, null, 2));

// convert order by tree
let arr2Order = test.array2SortByTree(arr, "id", "parent");
console.log("array to Order -->\n", JSON.stringify(arr2Order, null, 2));

// sum and percent weigth in tree 
let arrOrderWeight = test.array2SortAndWeight(arr, "id", "parent", "total");
console.log("array to Order -->\n", JSON.stringify(arrOrderWeight, null, 2));

Convert tree structure into Array:

const test = require("cng-tree-utils");

let arrOrigin = 
    [
        {
            "id": 1,
            "name": "A",
            "total": 0,
            "subs": [
                {
                    "id": 4,
                    "parent": 1,
                    "name": "A.1",
                    "total": 3
                }
            ]
        },
        {
            "id": 2,
            "name": "B",
            "total": 1,
            "subs": [
                {
                    "id": 3,
                    "parent": 2,
                    "name": "B.1",
                    "total": 2,
                    "subs": [
                        {
                            "id": 6,
                            "parent": 3,
                            "name": "B.1.1",
                            "total": 5
                        }
                    ]
                },
                {
                    "id": 5,
                    "parent": 2,
                    "name": "B.2",
                    "total": 4
                }
            ]
        }
    ]

// Print out Origin tree
console.log("Origin ARRAY ***>\n", JSON.stringify(arrOrigin, null, 2));

// Convert tree to array
let tree2Arr = test.tree2Array(arrOrigin, "subs");
console.log("\n*************>\ntree to ARRAY -->\n", tree2Arr);

Hope to help you in handling flat tree data types like in SQL and NoSQL database structures, like sqlite2, mongodb, oracle, mySql without having to use sql clauses inside database.