1.1.0 • Published 8 years ago

o-unflatten v1.1.0

Weekly downloads
3
License
UNLICENSED
Repository
github
Last release
8 years ago

unflatten

Version Downloads

Package for conversion from flat to nested objects

Installation

npm install --save o-unflatten

Usage

const nodes = [
	{id: 1, parentId: 0},
	{id: 2, parentId: 0},
	{id: 3, parentId: 1},
	{id: 4, parentId: 1},
	{id: 5, parentId: 2},
	{id: 6, parentId: 4},
	{id: 7, parentId: 5}
];
const unflatten = require('o-unflatten');
const nested = unflatten(nodes);
console.log(nested);

Input

const nodes = [
	{id: 1, parentId: 0},
	{id: 2, parentId: 0},
	{id: 3, parentId: 1},
	{id: 4, parentId: 1},
	{id: 5, parentId: 2},
	{id: 6, parentId: 4},
	{id: 7, parentId: 5}
];

Output

const nested = [
	{
    	"id": 1,
    	"parentId": 0,
    	"children": [
        	{
            	"id": 3,
            	"parentId": 1
        	},
        	{
            	"id": 4,
            	"parentId": 1,
            	"children": [
                	{
                    	"id": 6,
                    	"parentId": 4
                	}
            	]
        	}
    	]
	},
	{
    	"id": 2,
    	"parentId": 0,
    	"children": [
        	{
            	"id": 5,
            	"parentId": 2,
            	"children": [
                	{
                    	"id": 7,
                    	"parentId": 5
                	}
            	]
        	}
    	]
	}
];

Tests

npm run test
1.1.0

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago