4

Horrible nested array:

x=[0,[1,[2,[3,[4,["five"],[5]]]],["to",["free",["floor"]]],["two",["three",["four"]]]]]

Output of the magic function that I would like:

magic(x)=>[[0,1,2,3,4,"five"],[0,1,2,3,4,5],[0,1,"to","free","floor"],[0,1,"two","three","four"]]

So far with:

 magic(x)==>
 this.unpack_gret = function(strut){
                var lenstruc = strut.length;
                var firstelem = strut.shift();
                if (lenstruc > 1){

                    var maprecur = strut.map(function(item){
                        var retrecur = [firstelem].concat(this.unpack_gret(item))
                        return retrecur
                    });
                    if (maprecur.length > 1){return maprecur}
                    else {return maprecur[0];}

                }
                else {
                    return [firstelem];
              };
            };

I get:

[0,[1,2,3,[4,"five"],[4,5]],[1,"to","free","floor"],[1,"two","three","four"]]

Not bad but not there either. Any ideas?

6
  • 1
    If possible, using underscorejs.org/#flatten this turns into a no brainer. Commented May 21, 2014 at 13:49
  • @limelights flatten does not do what OP is after... Commented May 21, 2014 at 13:51
  • Fair enough.... but I don't want to have to serve a whole library just to do one function. Commented May 21, 2014 at 13:51
  • @epascarello no, not by itself, but with some creative use :P Commented May 21, 2014 at 13:53
  • 2
    Should the last array in your example start with 0? Commented May 21, 2014 at 13:53

1 Answer 1

4

First let's pretty-print your input array, just so I can see what I'm doing.

x =
[
  0,
  [
    1,
    [
      2,
      [
        3,
        [
          4,
          ["five"],
          [5]
        ]
      ]
    ],
    [
      "to",
      [
        "free",
        ["floor"]
      ]
    ],
    [
      "two",
      [
        "three",
        ["four"]
      ]
    ]
  ]
]

Okay. so basically it's a tree and you want an array of all the branches. Shouldn't be too hard...

function branchify(arr,branch,found) {
    branch = branch || [];
    found = found || [];

    var leaf = arr.shift();
    branch.push(leaf);
    if( arr.length == 0) {
        found.push(branch);
    }
    else {
        arr.forEach(function(path) {
            branchify(path,branch.slice(0),found);
        });
    }
    return found;
}
var result = branchify(x);

Demonstration

"Ta-da!", as they say.

Sign up to request clarification or add additional context in comments.

5 Comments

Like a boss! Thank you very much.
If the last array had multiple children how would i handle this?
Say the array looked like this x=[0,[1,[2,[3,[4,["five","fiveb"],[5,5]]]],["to",["free",["floor","phloor"]]],["two",["three",["four","phour"]]]]]
That's not a valid array within the specification you gave of what your array would look like.
Its an even more generic case.

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.