objectree v1.1.1
objectree
About
Objectree is a javascript node.js module that can convert a javascript or JSON object to a tree and access as a tree structure.
Installation
npm install objectreeUsage
var objectree = require("objectree");
var obj = {
"a": "test",
"b": true,
"c": 20,
"d": {
"e": {
"f": "test_a",
"g": "test_b"
}
}
};
var fullTree = objectree.createTree(obj);
.....Features
- A
treestructure is created from javascript/json object. treeis a node that containschildandparentconcept- Loop through the structure with
childandparentfacilities. - Represent a Tree structured node.
Documentation
Methods
createTreenewNodehasChildhasAttributegetNameaddChildgetChildsgetChildByIndexgetFirstChildaddAttributegetAttributesetParentgetParentsetValuegetValuehasSiblingisLeafserialize
createTree()
Create a tree node of the given obj to have the tree structure
Arguments
obj- A javascript object that is need to be converted to tree structure.
Returns
Tree object
Examples
// obj -> is a javascript object.
var treeObject = objectree.createTree(obj);newNode()
Explicitly create a new Node to add or remove.
Arguments
void
Returns
Node
Examples
var newNode = objectree.newNode();hasChild()
To check if the object has any child
Arguments
void
Returns
trueorfalse
Examples
var hasChild = treeObject.hasChild();hasAttribute()
To check if the object has any attributes
Arguments
void
Returns
trueorfalse
Examples
var hasAttribute = treeObject.hasAttribute();getName()
Get name of the current node.
Arguments
void
Returns
stringname
Examples
var name = treeObject.getName();addChild()
Add a node to current node child list
Arguments
Nodeto add as a child
Returns
void
Note
- Get a child from node or create a node with
objectree.newNode()
Examples
test.addChild(treeObject.getFirstChild()); getChilds()
Get all childs list of current node.
Arguments
voidNo argument
Returns
child list
Examples
var childs = test.getChilds(); getFirstChild()
Get first child of current node.
Arguments
void
Returns
Nodefirst child ornull
Examples
var firstChild = test.getFirstChild(); getChildByIndex()
Get child by index number
Arguments
number0 based
Returns
Nodechild of given index or 'null'
Examples
var child = test.getChildByIndex(2); addAttribute()
Add attribute to current node.
Arguments
ArrayorStringorNumberObjectandfunctionare not supported
Returns
void
Examples
test.addAttribute('abcd'); getAttribute()
Add a node to current node child list
Arguments
void
Returns
attribute listornull
Examples
var attributes = test.getAttribute(); setParent()
Set parent of current node explicitly. ( Not suggested )
Arguments
Node
Returns
void
Examples
var newParent = objectree.newNode();
treeObject.setParent(newParent);getParent()
Get parent node of current node.
Arguments
void
Returns
- parent
Nodeornull
Examples
var parent = treeObject.getParent();setValue()
Set value of current node.
Arguments
StringorBooleanorNumberfunctionandObjectis not supported
Returns
void
Examples
treeObject.setValue("Hello World");getValue()
Get value of current node.
Arguments
void
Returns
Valueornull
Examples
var value = treeObject.getValue();hasChild()
To check if the object has any child
Arguments
void
Returns
trueorfalse
Examples
var hasChild = treeObject.hasChild();hasSibling()
To check if current node has any sibling or not
Arguments
void
Returns
trueorfalse
Examples
var hasSibling = treeObject.hasSibling();isLeaf()
To check if the object is leaf
Arguments
void
Returns
trueorfalse
Examples
var isLeaf = treeObject.isLeaf();serialize()
Serialize the object before sending over or string conversion.
Arguments
void
Returns
String
Examples
var isLeaf = treeObject.serialize();