Last active
May 26, 2016 13:03
-
-
Save MeyCry/19b5a412235538d291f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
category = [ | |
{id: "animals", parent: null} | |
{id: "mammals", parent: "animals"} | |
{id: "cats", parent: "mammals"} | |
{id: "dogs", parent: "mammals"} | |
{id: "alliance", parent: null} | |
{id: "labrador", parent: "dogs"} | |
{id: "chihuahua", parent: "dogs"} | |
{id: "siamese", parent: "cats"} | |
{id: "persian", parent: "cats"} | |
{id: "ee", parent: "dare"} | |
{id: "sdw", parent: "ee"} | |
{id: "dare", parent: null} | |
{id: "animals1", parent: null} | |
{id: "animals2", parent: "animals1"} | |
{id: "animals3", parent: "animals2"} | |
{id: "animals4", parent: "animals3"} | |
{id: "animals5", parent: "animals4"} | |
{id: "animals6", parent: "animals5"} | |
{id: "animals7", parent: "animals6"} | |
{id: "animals8", parent: "animals7"} | |
{id: "animals9", parent: "animals8"} | |
{id: "animals10", parent: "animals9"} | |
{id: "animals11", parent: "animals10"} | |
{id: "animals12", parent: "animals11"} | |
{id: "animals13", parent: "animals12"} | |
{id: "animals14", parent: "animals13"} | |
{id: "animals15", parent: "animals14"} | |
] | |
### | |
EXPECT result | |
{ | |
"animals": { | |
"mammals": { | |
"cats": { | |
"siamese": {}, | |
"persian": {} | |
}, | |
"dogs": { | |
"labrador": {}, | |
"chihuahua": {} | |
} | |
} | |
}, | |
"alliance": {}, | |
"dare": {}, | |
"animals1": { | |
"animals2": { | |
"animals3": { | |
"animals4": { | |
"animals5": { | |
"animals6": { | |
"animals7": { | |
"animals8": { | |
"animals9": { | |
"animals10": { | |
"animals11": { | |
"animals12": { | |
"animals13": { | |
"animals14": { | |
"animals15": {} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
### | |
makeTree1 = (categories, parent) -> | |
obj = {} | |
categories.forEach (item) -> | |
if item.parent is parent | |
obj[item.id] = makeTree1(categories, item.id) | |
obj | |
makeTree2 = (categories, parent) -> | |
node = {} | |
categories | |
.filter (item) -> parent is item.parent | |
.forEach (item) -> | |
node[item.id] = makeTree2(categories, item.id) | |
return node | |
makeTree3 = (categories) -> | |
_nodes = {} | |
nodes = {} | |
for item in categories | |
_item = {} | |
if not item.parent | |
nodes[item.id] = _item | |
if item.parent not of _nodes | |
_nodes[item.parent] = {} | |
if item.id not of _nodes | |
_nodes[item.id] = _nodes[item.parent][item.id] = _item | |
else | |
_nodes[item.parent][item.id] = _nodes[item.id] | |
return nodes | |
JSON.stringify( makeTree1(category, null), null, 2) | |
JSON.stringify( makeTree2(category, null), null, 2) | |
JSON.stringify( makeTree3(category), null, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment