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 🙏

© 2025 – Pkg Stats / Ryan Hefner

treeify-js

v1.0.8

Published

Convert an array to a tree object.

Downloads

497

Readme

treeify.js - v1.0.8(under MIT)

Build Unit Tests Coverage npm bundle size License

English | 简体中文

What's treeify?

treeify.js is a tool for converting an array which each element contains the same specific rules into an object.

Installation

$ npm install --save-dev treeify-js

CDN

[unpkg]  https://unpkg.com/treeify-js

Build & Test

Packages gulp and mocha should already be installed globally. You should also run $ npm install first to install all dependencies.

Run the build script to build, and the test script to run unit test.

$ npm run build

Usage

So, now we have an array:


var arr = [
    {
        id: 'a1',
        parentId: 'a'
    },
    {
        id: 'aq',
        parentId: 'a2'
    },
    {
        id: 'a2',
        parentId: 'a'
    },
    {
        id: 'a1-1',
        parentId: 'a1'
    },
    {
        id: 'a',
        parentId: ''
    }
];

Then, You need to convert to such a tree object:


    {
        id: 'a',
        parentId: '',
        children: [
            {
                id: 'a1',
                parentId: 'a',
                children: [
                    {
                        id: 'a1-1',
                        parentId: 'a1',
                        children: []
                    }
                ]
            },
            {
                id: 'a2',
                parentId: 'a',
                children: [
                    {
                        id: 'aq',
                        parentId: 'a2',
                        children: []
                    }
                ]
            }
        ]
    }
    

You just need to give the array to treeify:

import { treeify } from 'treeify-js'

treeify(arr);

Then, you get what you want, It's an easy work.

Check the unit tests to get more usage.

Enjoy!

API

  • treeify(data, configure)

    Convert an array to a tree object. An array which as data source, all element of it must be an object that has an unique ID and a parent's ID.

    • data Array - Give a data which Array type to treeify
    • configure Object - A Object that Configure keys name mapping in convertion. The most common usage is no any configuration, just give the data along.
      • id: string|Function - default value is "id".
      • parentId: string|Function - default value is "parentId".
      • children: string|Function - default value is "children".
      • root: any type - specify a value directly, the value can be any type, if return a value which an array type, it means 'contains', such as { root: () => ["xxxx"] }, it is the same as { root: () => "xxxx" } if only one value is returned. default value is null.
      • multi: boolean - If the 'multi' is set * to TRUE, it can have multiple roots, FALSE can only have one, default value is false.
      • deepClone: boolean - Whether deep clone all elements of data in convert, default value is false.
  • untreeify(tree, childrenName)

    Convert a tree from treeify() back into an array. This is the inverse of treeify.

    • tree Object - A tree comes from treeify
    • childrenName String|Function - Give the children's key name, it can also be a function. default value is "children"

License

treeify is under MIT licensed.