How to loop through Object and create a tree Object

javascript, loops, object

Solution

I think the best solution here is a loop with some recursion. I have increased the size of the model in the example to show it going with n levels. Check the output with your javascript console.

var choices = ['choice1', 'choice2', 'choice3'];
var items = [{
    choice1: 'taste',
    choice2: 'good',
    choice3: 'green-lemon'
}, {
    choice1: 'taste',
    choice2: 'bad',
    choice3: 'green-lemon'
},
{
    choice1: 'taste',
    choice2: 'ok',
    choice3: 'green-lemon'
},
{
    choice1: 'taste',
    choice2: 'ok',
    choice3: 'green-lemon'
}];

function IsLastLevel(levelIndex) {
    return (levelIndex == choices.length - 1);
}

function HandleLevel(currentItem, currentLevel, nextChoiceIndex) {

    var nextLevelName = currentItem[choices[nextChoiceIndex]];

    if (typeof currentLevel[nextLevelName] === 'undefined') {
        currentLevel[nextLevelName] = {};
    }

    if (IsLastLevel(nextChoiceIndex)) {
        if (currentLevel[nextLevelName] > 0) {
            currentLevel[nextLevelName]++;
        } else {
            currentLevel[nextLevelName] = 1;
        }
    } else {
        var goOneDeeper = nextChoiceIndex + 1;
        HandleLevel(currentItem, currentLevel[nextLevelName], goOneDeeper);
    }
}

var output = {};

for(var itemIndex in items)
{
    var item = items[itemIndex];
    HandleLevel(item, output, 0);
}

console.log(output);

JsFiddle Demo

Problem

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?

Original source