Created
January 17, 2014 08:51
-
-
Save jcla1/8470279 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
function build_hierarchy(list, key, tuple_func) { | |
var direct_hierarchy = list | |
.map(tuple_func) | |
.reduce(function(obj, tuple) { | |
tuple[0] in obj ? obj[tuple[0]].push(tuple[1]) : obj[tuple[0]] = [tuple[1]]; | |
return obj; | |
}, {}); | |
var hierarchy = Object.keys(direct_hierarchy) | |
.reduce(function(obj, type) { | |
var entity = { Name: type }; | |
entity[key] = obj.input[type]; | |
obj.result.push(entity); | |
return obj; | |
}, {result: [], input: direct_hierarchy}).result; | |
return hierarchy; | |
} | |
var hierarchy = {"ProductHierarchy":{}}; | |
hierarchy["ProductHierarchy"]["Suppliers"] = build_hierarchy(data.ProductCollection, "Categories", function(a) { | |
return [a.SupplierName, a]; | |
}).map(function(a) { | |
a["Categories"] = build_hierarchy(a.Categories, "Products", function(a) { | |
return [a.Category, a]; | |
}); | |
return a; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment