3

I'm trying to combine and group an array with a bunch of flat arrays that contain only strings, no objects.

So my array looks something like this:

var array = [
    ["MotherNode", "Node1", "ChildNode1", "ChildOfChildNode1"],
    ["MotherNode", "Node1", "ChildNode2", "ChildOfChildNode2"],
    ["MotherNode", "Node2", "ChildNode3", "ChildOfChildNode3"],
    ["MotherNode", "Node2", "ChildNode3", "ChildOfChildNode4"],
    ["MotherNode", "Node3", "ChildNode4", "ChildOfChildNode5"],
    ["MotherNode", "Node3", "ChildNode4", "ChildOfChildNode5"]
]

Im doing this in javascript/angularjs and so far I've gathered that the best solution is probably to use underscore.js groupBy/combine methods. However most of the examples that i can find are dealing with arrays of objects where they can group them together by using a value's key. And I'm not good enough with algorithms yet to be able to figure this out on my own.

The array I'm dealing with can have hundreds of values and the result array could get 5-10 levels deep.

The result I'd like by parsing the above array would look something like this:

var result= {
    "MotherNode": [{
        "Node1":[{
            "ChildNode1":"ChildOfChildNode1"
            },{
            "ChildNode2":"ChildOfChildNode2"
        },{
        "Node2":[{
            "ChildNode3":["ChildOfChildNode3","ChildOfChildNode4"]
        },{
        "Node3":[{
            "ChildNode4":"ChildOfChildNode5"
        }
    ]
}

So does anyone have any clue how this can be done? I'm completely out of ideas.

6
  • possible duplicate of Underscore.js groupBy multiple values Commented Jun 24, 2014 at 20:26
  • Your syntax is messed up. Your array must not have indices, and the innermost objects of your result don't have any keys. Commented Jun 24, 2014 at 20:30
  • Why would your result consist of so many arrays that contain a single object only? Commented Jun 24, 2014 at 20:30
  • It's just what I imagine the result would look like where there is only a single child node. Didn't want to over complicate the example by having too many values since it would get incomprehensible pretty fast. I'll fix the syntax. Commented Jun 24, 2014 at 20:38
  • I checked http://stackoverflow.com/q/10022156/1048572 before i created this question. The difference here and there is that question is dealing with objects with keys so they can be grouped easily by using them. My problem is that there are no keys and i must group by values instead. Commented Jun 24, 2014 at 20:41

1 Answer 1

2

I solved this using _.reduce grouping wasnt the way to go

   var result = _.reduce(array,function(memo, val){ 
        var tmp = memo;
            _.each(val, function(fldr){
                if(!_.has(tmp, fldr)){
                    tmp[fldr] = {}
                }
                tmp = tmp[fldr]
            })

        return memo
    },{})

the end leaf wont be set as a value but it should be easy to change that behavior to whatever suits you use case

{ MotherNode: 
   { Node1: 
      { ChildNode1: { ChildOfChildNode1: {} },
        ChildNode2: { ChildOfChildNode2: {} } },
     Node2: { ChildNode3: { ChildOfChildNode3: {}, ChildOfChildNode4: {} } },
     Node3: { ChildNode4: { ChildOfChildNode5: {} } } } }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.