Array showing different included elements:
var exampeInclude= [
"Product",
"Group",
"Product.Tax",
"Product.ProductUnit",
"Product.Variant.Original",
"Product.Variant.Original.Tax",
"Product.Variant.Original.ProductUnit"
];
Desired hierarchical structure from the array:
[
{
"Product": {
"includes": [
{
"Tax": {
"includes": []
}
},
{
"ProductUnit": {
"includes": []
}
},
{
"Variant": {
"includes": [
{
"Original": {
"includes": [] //...
}
}
]
}
}
]
}
},
{
"Group": {
"includes": []
}
}
]
Attempting to achieve a tree-like structure but facing challenges with debugging and iteration. Progress made using a simple reduce function, however struggling with implementing the split('.')
method. Is recursion necessary for this task?
Current approach using reduce function:
var hist = exampeInclude.reduce(function (prev, item) {
if( item in prev ) prev[item] ++;
else prev[item] = 1;
return prev;
}, {});
(Basic counting functionality implemented using reduce function as starting point)