Skip to content Skip to sidebar Skip to footer

Arranging Elements In To A Hash Array

I am trying to break a javascript object in to small array so that I can easily access the innerlevel data whenever I needed. I have used recursive function to access all nodes ins

Solution 1:

No, you reduce the Facets to a string named html - but you want an object.

function generateList(facets) {
    var map = {};
    (function recurse(arr) {
        var join = [];
        for (var i=0; i<arr.length; i++) {
            var current = arr[i].term; // every object must have one!
            current = current.replace(/ /g, "_");
            join.push(current);  // only on lowest level?
            if (current in arr[i]) 
                map[current] = recurse(arr[i][current]);
        }
        return join;
    })(facets)
    return map;
}

Demo on jsfiddle.net

To get the one-level-data, you could just add this else-statement after the if:

            else
                map[current] = [ current ]; // create Array manually

Altough I don't think the result (demo) makes much sense then.


Post a Comment for "Arranging Elements In To A Hash Array"