I have a flat object and an array from which I need to construct a tree-like object.
choices: ['choice1', 'choice2', 'choice3'];
items: [
{
choice1: 'taste',
choice2: 'good',
choice3: 'green-lemon'
},
{
choice1: 'taste',
choice2: 'bad',
choice3: 'green-lemon'
}
];
The array describes the level at which each choice will come in the tree. I do not know how many choices, items or levels there will be later.
How do I get the following object:
output: {
taste: {
good: {
green-lemon:1
},
bad: {
green-lemon:1
}
}
}
I need to get an object describing how many items there are on each level. In this example this is choice1: 1; choice2: 2 and each choice3: 1.
Any advice on how to build a loop to get this result?
choicesarray meant to describe the level at which each choice comes in the tree? @brainwipe's question is also relevant to a correct solution.