https://github.com/ubcent/unflatten
Package for flat to nested objects conversion
https://github.com/ubcent/unflatten
Last synced: about 1 year ago
JSON representation
Package for flat to nested objects conversion
- Host: GitHub
- URL: https://github.com/ubcent/unflatten
- Owner: ubcent
- License: mit
- Created: 2016-06-21T09:44:08.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-24T07:19:38.000Z (over 8 years ago)
- Last Synced: 2025-02-04T20:38:00.618Z (over 1 year ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# unflatten
[](https://www.npmjs.com/package/o-unflatten)
[](https://www.npmjs.com/package/o-unflatten)
Package for conversion from flat to nested objects
## Installation
```sh
npm install --save o-unflatten
```
## Usage
```javascript
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
```javascript
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
```javascript
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
```sh
npm run test
```