array-to-tree
Advanced tools
Comparing version
32
index.js
'use strict'; | ||
var isArray = require('isarray'); | ||
var extend = require('shallow-object-extend'); | ||
var exists = require('property-exists'); | ||
var exists = function(obj, key) { | ||
return obj != null && Object.hasOwnProperty.call(obj, key); | ||
}; | ||
var extend = function(destination, source) { | ||
for (var property in source) { | ||
destination[property] = source[property]; | ||
} | ||
return destination; | ||
} | ||
var isArray = Array.isArray || function(obj) { | ||
return toString.call(obj) === '[object Array]'; | ||
}; | ||
var createTree = function(array, rootNodes, customID) { | ||
var tree = []; | ||
for (var key in rootNodes) { | ||
if (!exists(rootNodes, key)) { | ||
for (var rootNode in rootNodes) { | ||
if (!exists(rootNodes, rootNode)) { | ||
continue ; | ||
} | ||
var parentNode = rootNodes[key]; | ||
var childNode = array[parentNode[customID]]; | ||
var node = rootNodes[rootNode]; | ||
var childNode = array[node[customID]]; | ||
if (childNode) { | ||
parentNode.children = createTree(array, childNode, customID); | ||
node.children = createTree(array, childNode, customID); | ||
} | ||
tree.push(parentNode); | ||
tree.push(node); | ||
} | ||
@@ -56,3 +44,3 @@ | ||
* arrayToTree | ||
* Convert a plain array of nodes (with pointers to parent nodes) to a tree | ||
* Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure | ||
* | ||
@@ -59,0 +47,0 @@ * @name arrayToTree |
{ | ||
"name": "array-to-tree", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Convert a plain array of nodes (with pointers to parent nodes) to a tree", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
}, | ||
"repository": "alferov/array-to-tree", | ||
"repository": "alferov/property-exists", | ||
"keywords": [ | ||
@@ -33,3 +33,8 @@ "array", | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"isarray": "0.0.1", | ||
"property-exists": "^1.0.1", | ||
"shallow-object-extend": "0.0.1" | ||
} | ||
} |
# array-to-tree [](https://travis-ci.org/alferov/array-to-tree) | ||
> Convert a plain array of nodes (with pointers to parent nodes) to a tree. | ||
> Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure. | ||
Solves a problem with conversion of retrieved from a database sets of data to a nested structure (i.e. navigation tree). | ||
Solves a problem with conversion of retrieved from a database sets of data to a nested data structure (i.e. navigation tree). | ||
@@ -91,3 +91,3 @@ ## Install | ||
### `arrayToTree(options)` | ||
Convert a plain array of nodes (with pointers to parent nodes) to a tree. | ||
Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure. | ||
@@ -94,0 +94,0 @@ #### Params |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9435
-0.64%3
Infinity%184
-4.66%+ Added
+ Added
+ Added
+ Added