array-tree-transform
v1.0.4
Published
function arrayToTree and treeToArray
Downloads
4
Readme
array-tree-transform
EN: Creating a tree based on an flatten array of items with fields "id" and "parent_id". And vice versa, creating an array from tree.
RU: Создание дерева на основе плоского массива записей с полями "id" и "parent_id". И обратно, создание массива из дерева.
Installation
npm install --save array-tree-transform
Usage
import { arrayToTree, treeToArray } from 'array-tree-transform';
var dataOne = [
{
id: 1,
name: 'Portfolio',
parent_id: 4
},
{
id: 2,
name: 'Web Development',
parent_id: 1
},
{
id: 3,
name: 'Recent Works',
parent_id: 2
},
{
id: 4,
name: 'About Me',
parent_id: undefined
}
];
var tree = arrayToTree(dataOne, function(parent, child) {
return parent.id === child.parent_id;
});
console.log(tree);
var arr = treeToArray(tree);
console.log(arr);